class A { public int a = 100; } // End of class A
class B extends A { public int a = 80; } // End of class B
class C extends B { public int a = 60; } // End of class C
class D extends C { public int a = 40; } // End of class D
class E extends D{
public int a =10;
public void show(){
int a =0;
// Write Java statements to display the values of
// all a’s used in this file on System.out
} // End of show() Method
}// End of class E
我正在尝试从E访问超类A,B,C的变量a,但我不知道该怎么做。我尝试过这样的事情
System.out.println(super.a+" "+super.super.a+" "+super.super.super.a+" "+
super.super.super.super.a);
答案 0 :(得分:1)
由于A,B,C,D是E的超类,我们可以将E强制转换为超类型并访问该字段。
System.out.println(((A) this).a); //Prints 100
System.out.println(((B) this).a); //Prints 80
System.out.println(((C) this).a); //Prints 60
System.out.println(((D) this).a); //Prints 40