java中的继承与多态总结

来源:互联网 发布:新网络武侠小说排行榜 编辑:程序博客网 时间:2024/05/20 19:48

publicclassT001{

publicstaticvoidmain(String[]args){

newPerson().printPerson();

newStudent().printPerson();

}

}



classStudentextendsPerson {

privateStringgetInfo(){

return"student";

}

}


classPerson {

privateString getInfo() {

return"preson";

}

publicvoidprintPerson() {

System.out.println(getInfo());

}

}




父类的private方法无法被重写。Student类中的getInfo方法没有被使用。因为Student类中的getInfo方法无法重写Person类中的getInfo方法。所以Student类中存在两个getInfo方法。Student类继承来的printPerson方法在调用getInfo方法时选择了同样继承自Person类的getInfo,而没有选择Student类中原生的getInfo方法。