java动态绑定的一点注意

时间:2023-03-09 15:34:51
java动态绑定的一点注意

动态绑定只是针对对象的方法,对于属性无效。因为属性不能被重写。

show me code:

public class Father{
public String name = "父亲属性";
}
public class Son extends Father{
public String name = "孩子属性"; public static void main(String[] args){
Father instance = new Son();
System.out.println(instance.name);
}
}
//结果:父亲属性