class Father {
private String name = "Father";
public String getName() {
return name;
}
}
class Child extends Father{
private String name = "child";
public static void main(String[] args) {
Child c = new Child();
System.out.println("输出结果:"+c.getName());
}
}
输出结果:Father
————————————————
当一个方法只在父类中定义时,为什么调用该方法时会使用父类中的属性?子类和父类有同名的属性,调用父类方法,用的属性为什么不是子类的?