如何(((GrandParent)this).x))能够打印GP类的x的值,如何在堆中创建继承对象之后。它如何存储祖父母和父母数据。
class GrandParent
{
int x = 34;
}
class Parent extends GrandParent
{
int x =87;
}
class Child extends Parent
{
int x =536;
void show()
{
System.out.println("value of local x "+x);
System.out.println("value of Parent x "+super.x);
System.out.println("value of GrandParent x "+(((GrandParent)this).x));
}
public static void main(String... s)
{
Child c1 = new Child();
c1.show();
}
}
输出
value of local x 536
value of Parent x 87
value of GrandParent x 34