次のコード例では:
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(p.x + " " + c.x);
System.out.println(p.aMethod() + " \n");
System.out.println(c.aMethod() + " \n");
}
}
そして出力:
5 6
Child.aMthod 6
Child.aMthod 6
p.aMethod()
px が 6 を出力するときに 6 を出力しないのはなぜですか?
ありがとう
編集
おっとちょっとしたタイプミス: 質問は「なぜ px が 5 を印刷するときに p.aMethod() が 5 を印刷しないのか」であるべきです - @thinksteep に感謝します