出力の理由を教えてください。
私によると、 b.getx() を使用すると、B のオブジェクトの参照 ID を取得しb.getx().x
、10 の値を取得する必要がありますが、このプログラムを実行すると、出力は 5 になります。
class Base {
int x = 5;
public Base getx() {
return new Base();
}
}
class Child extends Base {
int x = 10;
public Child getx() {
return new Child();
}
public static void main(String ...s) {
Base b = new Child();
System.out.println(b.getx().x);
}
}