publicclassPeople{publicvoidtalk(){
System.out.println("I'm a Human!!");}}
publicclassStudentsextendsPeople{publicvoidtalk(){
System.out.println("I'm a Student!!");}}
publicclassTestInherit{publicstaticvoidmain(String[] args){
People s1 =newStudents();//父类的引用指向子类
Students s2 =newStudents();
s1.talk();
s2.talk();
People people =newPeople();
people.talk();}}
I'm a Student!!
I'm a Student!!
I'm a Human!!
一个对象的实际类型(eg:People s1 = new Students())是确定的,但是它的引用类型(eg:People s1 = new Students())是不确定的。比如上方代码中,s1和s2的实际类型都是Students,而s1的引用类型是People,而s2的引用类型是Students。
目录什么是多态注意事项什么是多态多态是指同一个行为具有多个不同表现形式和形态的能力多态是同一个接口(方法名?),使用不同的实例而完成不同的操作public class People { public void talk(){ System.out.println("I'm a Human!!"); }}public class Students extends People{ public void talk(){ System