在一个类中,与超类中的字段同名的字段会隐藏超类的字段。
public class Test {
public static void main(String[] args) {
Father father = new Son();
System.out.println(father.i); //why 1?
System.out.println(father.getI()); //2
System.out.println(father.j); //why 10?
System.out.println(father.getJ()); //why 10?
System.out.println();
Son son = new Son();
System.out.println(son.i); //2
System.out.println(son.getI()); //2
System.out.println(son.j); //20
System.out.println(son.getJ()); //why 10?
}
}
class Son extends Father {
int i = 2;
int j = 20;
@Override
public int getI() {
return i;
}
}
class Father {
int i = 1;
int j = 10;
public int getI() {
return i;
}
public int getJ() {
return j;
}
}
有人可以为我解释结果吗?
答案 0 :(得分:9)
在java中,字段不是多态的。
Father father = new Son();
System.out.println(father.i); //why 1? Ans : reference is of type father, so 1 (fields are not polymorphic)
System.out.println(father.getI()); //2 : overridden method called
System.out.println(father.j); //why 10? Ans : reference is of type father, so 2
System.out.println(father.getJ()); //why 10? there is not overridden getJ() method in Son class, so father.getJ() is called
System.out.println();
// same explaination as above for following
Son son = new Son();
System.out.println(son.i); //2
System.out.println(son.getI()); //2
System.out.println(son.j); //20
System.out.println(son.getJ()); //why 10?
答案 1 :(得分:3)
根据Overriding and Hiding Methods
被调用的隐藏方法的版本取决于它是从超类还是从子类调用。
即。当您通过超类引用调用在子类中重写的方法时,将调用超类方法并访问超类成员。
这解释了以下内容,因为所使用的参考是超类:
System.out.println(father.i); //why 1?
System.out.println(father.j); //why 10?
System.out.println(father.getJ()); //why 10?
同样如下:
System.out.println(son.getJ()); //why 10?
由于在getJ()
中未定义Son
,因此调用Father
版本,该版本会查看Father
类中定义的成员。
如果您阅读Hiding Fields;他们特别不推荐这种编码方法
一般来说,我们不建议隐藏字段,因为它会使代码难以阅读。