匿名内部类在开发中的应用
public class test1_NoNameInner {public static void main(String[] args) {PersonDemo yy = new PersonDemo();//yy.method(new Student());yy.method(new Person() {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();/*Person p = new Person(){ //父类引用指向子类对象public void show(){ //将注释里面的这些东西赋值给Person p;System.out.println("show");}};*/ public void method(Person p){ //编译看的是父类show方法();p.show(); //运行看的是子类调用重写之后show方法;}
}class Student extends Person{public void show(){System.out.println("show");}
}