版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
特点:
匿名内部类只能被使用一次,(如果个类只使用一次可以通过匿名类来实现
)它就是用来实体化一个抽象类或者接口的,直接在方法里面实现抽象方法。
举例:
这个是实现抽象类
abstract class Person {
public abstract void eat();
}
public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}
这个是实现接口
interface Person {
public void eat();
}
public class Demo {
public static void main(String[] args) {
Person p = new Person() {
public void eat() {
System.out.println("eat something");
}
};
p.eat();
}
}