本文介绍了访问已隐藏在第三个扩展类中的间接超类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下代码:
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();
}
}
如何访问类中的a一个间接继承到C类。我知道我可以创建一个A类的对象,我也知道我可以在B类中创建一个方法来返回super.a(a变量A类,当然如果它是静态的,我可以像Aa那样访问它。
How can i access "a" of the "class A" indirectly inherited to "class C".I know i can create an object of the " class A ", i also know i can create a method in "class B" to return "super.a" ( "a" variable of the "class A"), of course if it were static i could have accessed it like "A.a".
如果有任何其他方法可以直接访问它我。
If there is any other method to access it directly kindly enlighten me.
(提前感谢)。
推荐答案
转换为A然后访问变量:
Cast to A and then access the variable:
((A)this).a
这篇关于访问已隐藏在第三个扩展类中的间接超类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!