比较下列2段代码的输出:
代码①
public class TestString {
public static void main(String[] args){
new Person().printPerson();
new Student().printPerson();
}
}
class Person{
public String getInfo(){
return "Person";
}
public void printPerson(){
System.out.println(getInfo());
}
}
class Student extends Person{
public String getInfo(){
return "Student";
}
}
代码②
public class TestString {
public static void main(String[] args){
new Person().printPerson();
new Student().printPerson();
}
}
class Person{
private String getInfo(){
return "Person";
}
public void printPerson(){
System.out.println(getInfo());
}
}
class Student extends Person{
private String getInfo(){
return "Student";
}
}
①
Person
Student
②
Person
Person
解释: