成员方法支持动态绑定(根据对象的实际类型确定执行那个方法), 而成员变量是静态绑定的(只根据对象的当前表示类型决定使用那个变量)
class Demo{
int count;
public Demo(int i){
System.out.println("Demo i = "+i);
count = i;
}
public String toString(){
return "Demo i = "+count;
}
}
class Parent{
public int s;
public Demo de;
public Parent(){
System.out.println("Parent");
s = 10;
de = new Demo(11);
}
public void f(){
System.out.println("s = "+s+" demo = "+de);
}
}
public class Child extends Parent{
public int s;
public Demo de;
public Child(){
System.out.println("Child");
s = 30;
de = new Demo(22);
}
public void f(){
System.out.println("child s = "+s+" demo = "+de);
}
public static void main(String [] args){
Parent p = new Child();
System.out.println(p.s);
System.out.println(p.de);
p.f();
((Parent)p).f();
System.out.println(((Parent)p).s);
System.out.println(((Parent)p).de);
System.out.println("++++++++++++++++++++");
Child c = new Child();
System.out.println(c.s);
System.out.println(c.de);
System.out.println(((Parent)c).s);
System.out.println(((Parent)c).de);
}
}