匿名内部类的基本实现

抽象类或接口 通过匿名内部类 进行实现,

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();
    }
}
 
############如果不是抽象类或接口,为pojo,那么也可以实现匿名内部类,重写父类方法,
匿名内部类就是重写父类或接口的方法,即将某方法进行特殊实现

posted on 2016-12-14 22:55  大兴兴  阅读(237)  评论(0编辑  收藏  举报

导航