public class Demo_Test5 {
public static void main(String[]args){
PersonDemo pd=new PersonDemo();
// pd.method(new Student());//父类引用指向之类对象,Person p=new Student();,编译看左边,运行看右边-多态
pd.method(new Person() {//匿名内部类,当做参数传递,本质把匿名内部类看做一个对象
@Override
public void show() {
System.out.println("匿名内部类重写show方法");
}
});
}
}
abstract class Person{
public abstract void show();
}
class PersonDemo{
// public void method(Person p){//Person p=new Student();
public void method(Person p){
p.show();
}
}
class Student extends Person{
@Override
public void show() {
System.out.println("重写父类Person中的show方法");
}
}