728x90
반응형
instanceof 연산자 ...........ㅠ 아직 헷갈린당
package example;
class GrandParent{
}
class Parent extends GrandParent{
}
class Child extends Parent{
}
public class Test01 {
public static void main(String[] args) {
GrandParent gp = new GrandParent();
Parent pt = new Parent(); //부모형 참조변수는 자식객체 바로 참조 가능
Child ch = new Child(); //자식형 참조변수는 부모객체 바로 참조 불가
//1. 조상클래스가 자신의 클래스를 참조하고 있는 경우.
System.out.println("GrandParent gp = new GrandParent();");
if(gp instanceof GrandParent)
System.out.println("gp가 참조하는 GrandParent는 GrandParent를 상속하고 있다.(자기자신이므로 true)");
if(gp instanceof Parent)
System.out.println("gp가 참조하는 GrandParent는 Parent를 상속하고 있다.");
if(gp instanceof Child)
System.out.println("gp가 참조하는 GrandParent는 Child를 상속하고 있다.");
//gp,pt,ch가 각각 자신의 클래스를 참조하고 있음
//따라서, 참조변수가 자신의 클래스를 참조하는 경우)
//참조변수 a가 B클래스로 자동형변환이 되는지 여부를 판단하는 거라고 생각하면 됨.
//단!!!! 참조변수가 자식 클래스를 참조하고 있다면 이야기가 달라짐.(아래 코드 참조)
//2.조상클래스가 손주 클래스를 참조하고 있는 경우.
GrandParent gp2 = new Child();
System.out.println("\nGrandParent gp2 = new Child();");
if(gp2 instanceof GrandParent)
System.out.println("gp가 참조하는 Child는 GrandParent를 상속하고 있다.");
if(gp2 instanceof Parent)
System.out.println("gp가 참조하는 Child는 Parent를 상속하고 있다.");
if(gp2 instanceof Child)
System.out.println("gp가 참조하는 Child는 Child를 상속하고 있다.(자기자신이므로 true)");
//gp가 손주격 인스턴스를 참조하니 모든 문장이 출력됨
//따라서,참조변수가 자신이 아닌 클래스를 참조하는 경우)
//결론 : 참조변수 a가 참조하는 인스턴스가, "B 클래스를 상속하는 클래스인가?"를 판단해줌
}
}
<결과>
GrandParent gp = new GrandParent();
gp가 참조하는 GrandParent는 GrandParent를 상속하고 있다.(자기자신이므로 true)
GrandParent gp2 = new Child();
gp가 참조하는 Child는 GrandParent를 상속하고 있다.
gp가 참조하는 Child는 Parent를 상속하고 있다.
gp가 참조하는 Child는 Child를 상속하고 있다.(자기자신이므로 true)
06.01 내용 추가
instanceof 연산자를 가지고 계속 테스트해보니 어느정도 개념이 잡히는 것 같다. instanceof 연산자와 클래스 간 대입을 결부시키면 더 헷갈리니까 차라리 따로 보는게 낫다.
instanceof 연산자는 그냥 키워드 뜻대로, a변수가 B클래스를 참조할 수 있는지를 알려준다.
package example;
class Cake{
}
class CheeseCake extends Cake{
}
class StCheeseCake extends CheeseCake{
}
public class Test01 {
public static void main(String[] args) {
Cake c1 = new Cake();
if(c1 instanceof StCheeseCake)
System.out.println("c1는 스트로베리 치즈 케이크 인스턴스 참조 가능");
if(c1 instanceof CheeseCake)
System.out.println("c1은 치즈 케이크 인스턴스 참조 가능");
if(c1 instanceof Cake)
System.out.println("c1은 케이크 인스턴스 참조 가능");
System.out.println(" ========================== ");
Cake c2 = new StCheeseCake();
if(c2 instanceof StCheeseCake)
System.out.println("c2는 스트로베리 치즈 케이크 인스턴스 참조 가능");
if(c2 instanceof CheeseCake)
System.out.println("c2는 치즈 케이크 인스턴스 참조 가능");
if(c2 instanceof Cake)
System.out.println("c2는 케이크 인스턴스 참조 가능");
}
}
<결과>
c1은 케이크 인스턴스 참조 가능
==========================
c2는 스트로베리 치즈 케이크 인스턴스 참조 가능
c2는 치즈 케이크 인스턴스 참조 가능
c2는 케이크 인스턴스 참조 가능
c1은 Cake(); 인스턴스를 참조하고 있다.
c2는 StCheeseCake(); 인스턴스를 참조하고 있다. StCheeseCake는 Cake과 CheeseCake를 상속한다. 그러므로 StCheeseCake 객체는 Cake와 CheeseCake의 멤버들을 다 포함하고 있다. 즉, StCheeseCake는 Cake와 CheeseCake 인스턴스 범위를 포함하고 있기 때문에! instanceof 연산자로 따졌을 때 모든 인스턴스가 참조가 가능한 것이다.
728x90
반응형
'study_java > java_수강정리(~10.29)' 카테고리의 다른 글
[JAVA]17일차 (0) | 2020.06.02 |
---|---|
[JAVA]16일차 (0) | 2020.06.01 |
[JAVA]15일차 (0) | 2020.05.31 |
[JAVA]14일차 (1) | 2020.05.31 |
[JAVA]13일차 (0) | 2020.05.27 |