2014-04-18 3 views
1

BA을 상속하며 DC을 상속합니다. D 인스턴스는 B 인스턴스와 함께 전달되지만 변수를 재정의하거나 새로운 참조를 생성하지 않고 D 유형은 bStuff()을 볼 수 없습니다. C 클래스의 멤버 변수 aA하지 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(); 
    } 
} 
+1

사람들과 이름이 같은 수업은 문자로 제공됩니다 !! !! – ryrich

+0

D.doStuff()에서'b.aStuff()'를 사용하지 않는 이유는 무엇입니까? – Vikdor

+0

@ryrich 'A'를 '1', 'B'를 '2'등으로 변경하려고 시도했지만 그때 aStuff() 및 bStuff()는 아무런 의미가 없었습니다 ...? – inyourcorner

답변

2

C의 서브 클래스가 A의 특정 서브 클래스 (즉 D 항상 B와 함께 작동)를, 당신이 C 제네릭 할 수 있습니다 작업을 해야하는 경우

class C<T extends A> { 
    T a; 
    C(T a) { 
     this.a = a; 
    } 
    ... 
} 

class D extends C<B> {  
    D(B b) { 
     super(b); 
    } 
    ... 
} 
+0

빙고. 너는 그것을 얻었다. – inyourcorner

1

.

그래서, 클래스 D의 방법 doStuff에, 당신은 단지 클래스 B에 존재 a에 어떤 방법을 호출 할 수 없습니다 - 방법은 a 정말 B을 말한다 것을 알고하지 않습니다.

당신은 (아주 좋은하지 않습니다) DdoStuff 방법에 캐스트를 수행하여이 문제를 얻을 수 있습니다, 또는 B에 클래스 Ca의 유형을 변경하여.

class D { 
    // ... 

    public void doStuff() { 
     ((B) a).bStuff(); 
    } 
} 
1

당신은 클래스 B의 방법을 ovverride 경우 , 나는 당신이 원하는 결과를 얻을 것이라고 생각합니다. class C에서 a 입력 A의 때문에

class A { 
    public void doStuff() { System.out.println("a-type stuff"); } 
} 

class B extends A { 
    public void doStuff() { System.out.println("b-type stuff"); } 
} 

class C { 
    A a; 
    C(A a) { 
     this.a = a; 
    } 
} 

class D extends C{ 
    D(B b) { 
     super(b); 
    } 

    public void doStuff() { 
     a.doStuff(); //calls B's doStuff method 
    } 

    public static void main(String[] args) { 
     new D(new B()).doStuff(); 
    } 
} 
1

은 그것을 볼 수 없습니다.

다음은 많은 장소에서 본 적이 있지만 여전히 도움이되는 예입니다.

class Animal 
{ 
    public void eat(){ 
     System.out.println("Animal eats something"); 
    } 
    public void organic(){ 
     System.out.println("Animals are organic"); 
    } 

} 
class Dog extends Animal 
{ 
    @Override 
    public void eat(){ 
     System.out.println("Dog eats something"); 
    } 
    public void eat(String food){ 
     System.out.println("Dog eats +"food); 
    } 
    public void bark(){ 
     System.out.println("Dog barks"); 
    } 
} 

Animal dog = new Dog(); 

dog.eat();     //Dog eats something 

((Animal)dog).eat();  //Dog eats something  

//Still we get Dog's eat(). There is no way you could call eat() method of Animal using this dog. 

((Dog)dog).eat("steak"); //Dog eats steak 

//To call methods only in the subclass we need to cast it to the Object type 

((Dog)dog).bark();   //Dog barks 

//Same as above 

dog.organic();    //Animals are organic 

//With no worries we could call methods in Animal (Reference type) 


Animal a = new Animal(); 
Dog d = (Animal)a; 

//compiles but fails at runtime. If the types are in the same Inheritance tree, compiler says OK. 

Animal a3 = new Animal(); 
Dog d1 = (Dog)a3;  

//compiles because compiler sees both Dog and Animal in same tree. But fails during runtime. 

Dog d = new Dog(); 
Animal a1 = d; 
Animal a2 = (Animal)d; 

//Both compiles and runs fine. 
관련 문제