package com.touch.Test;
//父类
class FatherClass {
public int value;
public void getValue(){
System.out.println("FatherClass: "+this.value);
System.out.println("this is:"+this);
}
}
//子类
class ChildClass extends FatherClass {
public int value = 100;
public void getValue(){
System.out.println("ChildClass: "+this.value);
System.out.println("this is:"+this);
super.getValue();
}
}
//孙子类,
class SunziClass extends ChildClass {
public int value = 200;
public void getValue(){
System.out.println("SunziClass: "+this.value);
System.out.println("this is:"+this);
super.getValue();
}
}
//这儿是调用的Main
public class TestInherit {
public static void main(String[] args) {
SunziClass cc = new SunziClass();
cc.getValue();
}
}
结果为:
SunziClass: 200
this is:com.touch.Test.SunziClass@2f57d162
ChildClass: 100
this is:com.touch.Test.SunziClass@2f57d162
FatherClass: 0
this is:com.touch.Test.SunziClass@2f57d162
原因是:属性是由编译期来决定的,不是像方法的重写那样,是运行期来决定。