[英]Why doesn't the method “printperson()” invoke the subclass's private method?
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
private String getInfo() {
return "Student";
}
}
class Person {
private String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
}
The answer is 答案是
Person
人
Person人
But when I change both the private access-specifiers to public, the result is 但是当我将两个私有访问说明都更改为public时,结果是
Person
人
Student学生
Why? 为什么? I can't understand.
我听不懂
Private methods are not visible in subclasses and therefore can't be overriden. 私有方法在子类中不可见,因此不能被覆盖。 So it always invokes a method from parent class.
因此,它总是从父类中调用一个方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.