super
super 참조변수
- this와 함께 객체를 참조할 수 있는 참조 변수이다
- 현재 객체의 바로 상위인 super클래스(부모클래스)를 참조할 수 있다
super 생성자
- super 생성자 -> super()
- 부모 클래스의 멤버변수를 초기화하는 역할을 수행
예시 코드
class People{
int a = 100;
public People() {
super();
}
}
class Student extends People{
int b = 200;
public Student() {
super();
}
}
public class Super {
public static void main(String[] args) {
Student st = new Student();
}
}
Random 클래스
Random 객체 생성
import java.util.Random;
class RANDOM {
Random ran = new Random();
}
Random을 이용한 가위바위보
import java.util.Random;
import java.util.Scanner;
public class MathMain03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random r1 = new Random();
String[] arr = { "가위", "바위", "보" };
while (true) {
System.out.println("---------------------------");
System.out.println("메뉴>1.게임하기 | 2.종료하기");
System.out.println("---------------------------");
int num = input.nextInt();
Out:
if (num == 1) {
System.out.println("가위바위보 입력(0:가위, 1:바위, 2:보)");
int play = input.nextInt();
int index = r1.nextInt(3);
if(play>2) {
System.out.println("잘못 입력했습니다");
break Out;
}
if (play == 0 && index == 1) {
System.out.println("컴퓨터 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else if (play == 1 && index == 2) {
System.out.println("컴퓨터 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else if (play == 2 && index == 0) {
System.out.println("컴퓨터 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else if (play == 1 && index == 0) {
System.out.println("사람 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else if (play == 2 && index == 1) {
System.out.println("사람 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else if (play == 0 && index == 2) {
System.out.println("사람 승" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
} else {
System.out.println("무승부" + " 사람:" + arr[play] + " 컴퓨터:" + arr[index]);
}
} else if (num == 2) {
System.out.println("프로그램을 종료합니다");
break;
} else {
System.out.println("잘못 입력했습니다");
}
}
}
}
출력
---------------------------
메뉴>1.게임하기 | 2.종료하기
---------------------------
1
가위바위보 입력(0:가위, 1:바위, 2:보)
2
컴퓨터 승 사람:보 컴퓨터:가위
---------------------------
메뉴>1.게임하기 | 2.종료하기
---------------------------
1
가위바위보 입력(0:가위, 1:바위, 2:보)
1
무승부 사람:바위 컴퓨터:바위