class Demo_NoNameInnerClass {
public static void main(String[] args) {
StudentDemo sd = new StudentDemo();
sd.method(new Person() { //直接传一个继承了Person类的子类的匿名对象
public void eat() {
System.out.println("吃饭");
}
});
}
}
abstract class Person { //抽象类Person
public abstract void eat(); //抽象方法
}
class Student extends Person { //学生类继承Person类
public void eat() { //重写抽象方法
System.out.println("吃饭");
}
}
class StudentDemo {
public void method(Person p) { //形参列表是抽象类
p.eat();
}
} |
|