1、实例的属性的值取父类还是子类并不取决于创建对象的类型,而是取决于变量定义的类型。
例:
public class Father{
public String value="Father";
}
public class Son extends Father{
public String value="Son ";
public static void main(String args[]){
Father f=new Son();
System.out.println(f.value);//输出"Father"
}
}
2、在成员函数中访问成员变量时,成员变量的值取父类还是子类取决于是调用父类还是子类的成员函数。
public class Father{
public String value="Father";
public String getValule(){
return this.value;
}
public String getValule2(){
return this.value;
}
}
public class Son extends Father{
public String value="Son ";
public String getValule2(){
return this.value;
}
public static void main(String args[]){
Son s=new Son();
System.out.println(s.value);//输出"Son"
System.out.println(s.getValue());//输出"Father"
System.out.println(s.getValue2());//输出"Son"
}
}