隐藏的字段虽然inheritance

在以下代码示例中:

class Parent { int x =5; public Integer aMethod(){ System.out.print("Parent.aMthod "); return x; } } class Child extends Parent { int x =6; public Integer aMethod(){ System.out.print("Child.aMthod "); return x; } } class ZiggyTest2{ public static void main(String[] args){ Parent p = new Child(); Child c = new Child(); System.out.println(px + " " + cx); System.out.println(p.aMethod() + " \n"); System.out.println(c.aMethod() + " \n"); } } 

并输出:

 5 6 Child.aMthod 6 Child.aMthod 6 

当px打印6时,为什么p.aMethod()不打印6?

谢谢

编辑

哎呀一个小错字:问题应该是“为什么p.aMethod()在px print 5时不打印5” – 谢谢@thinksteep

当您访问类成员字段(实例变量)(如px )时,不会执行多态分辨率。 换句话说,您将从编译时已知的类中获得结果,而不是运行时已知的结果。

对于方法调用,这是不同的。 它们在运行时被分派给引用指向的实际类的对象,即使引用本身具有超类型。 (在VM中,这通过invokevirtual操作码发生,参见例如http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#invokevirtual )。

操作始终在对象上。 在这两种情况下, p.aMethod()c.aMethod() ,对象都是子,这就是为什么它在两种情况下都打印了6。 直接访问变量时,它会读取与左侧相关的变量。

因为声明变量不会inheritance。 您在类中有两个x副本,一个在父命名空间中,一个在子命名空间中。