访问已隐藏在第三个扩展 class 中的间接超级 class 变量
Accessing indirect super class variable that has been hidden in a third extended class
假设我有如下代码:
class A {
int a = 1;
}
class B extends A {
int a = 2;
}
class C extends B {
int a = 3;
void print_it() {
int a = 4; // Local variable "a" to the " print_it " method
System.out.println(a); //Should be 4
System.out.println(this.a); //Should be 3
System.out.println(super.a); //Should be 2
System.out.println("HOW DO I PRINT \" a \" OF THE \" CLASS A \" "); //I need to print 1
}
public static void main(String[] argue) {
C obj = new C();
obj.print_it();
}
}
我如何访问 "class A" 的 "a" 间接继承到 "class C"。我知道我可以创建“ class A ”的对象,我也知道我可以在 "class B" 到 return "super.a" 中创建一个方法("class A" 的 "a" 变量),当然如果它是静态的我可以访问它像 "A.a".
如果有其他方法可以直接访问,请赐教
(提前致谢)。
转换为 A 然后访问变量:
((A)this).a
假设我有如下代码:
class A {
int a = 1;
}
class B extends A {
int a = 2;
}
class C extends B {
int a = 3;
void print_it() {
int a = 4; // Local variable "a" to the " print_it " method
System.out.println(a); //Should be 4
System.out.println(this.a); //Should be 3
System.out.println(super.a); //Should be 2
System.out.println("HOW DO I PRINT \" a \" OF THE \" CLASS A \" "); //I need to print 1
}
public static void main(String[] argue) {
C obj = new C();
obj.print_it();
}
}
我如何访问 "class A" 的 "a" 间接继承到 "class C"。我知道我可以创建“ class A ”的对象,我也知道我可以在 "class B" 到 return "super.a" 中创建一个方法("class A" 的 "a" 变量),当然如果它是静态的我可以访问它像 "A.a".
如果有其他方法可以直接访问,请赐教
(提前致谢)。
转换为 A 然后访问变量:
((A)this).a