B
은 A
을 상속하며 D
은 C
을 상속합니다. D
인스턴스는 B
인스턴스와 함께 전달되지만 변수를 재정의하거나 새로운 참조를 생성하지 않고 D
유형은 bStuff()
을 볼 수 없습니다. C
클래스의 멤버 변수 a
형 A
하지 B
의 때문에, 당연히이다상속 된 필드를 다시 캐스팅합니다.
class Ryrich_A {
public void aStuff() { System.out.println("a-type stuff"); }
}
class Ryrich_B extends Ryrich_A {
public void bStuff() { System.out.println("b-type stuff"); }
}
class Ryrich_C {
Ryrich_A a;
Ryrich_C(Ryrich_A a) {
this.a = a;
}
}
class Ryrich_D extends Ryrich_C{
Ryrich_D(Ryrich_B b) {
super(b);
}
public void doStuff() {
a.aStuff();
// a.bStuff(); --problem--
}
public static void main(String[] args) {
new Ryrich_D(new Ryrich_B()).doStuff();
}
}
사람들과 이름이 같은 수업은 문자로 제공됩니다 !! !! – ryrich
D.doStuff()에서'b.aStuff()'를 사용하지 않는 이유는 무엇입니까? – Vikdor
@ryrich 'A'를 '1', 'B'를 '2'등으로 변경하려고 시도했지만 그때 aStuff() 및 bStuff()는 아무런 의미가 없었습니다 ...? – inyourcorner