首页 > 其他 > 详细

匿名内部类的基本实现

时间:2016-12-15 00:30:39      阅读:207      评论:0      收藏:0      [点我收藏+]

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

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,那么也可以实现匿名内部类,重写父类方法,
匿名内部类就是重写父类或接口的方法,即将某方法进行特殊实现

匿名内部类的基本实现

原文:http://www.cnblogs.com/tcdxx/p/6181471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!