码迷,mamicode.com
首页 > 其他好文 > 详细

匿名内部类的基本实现

时间:2016-12-15 00:31:01      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:ace   int   space   通过   public   keyword   方法   abs   x11   

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

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

匿名内部类的基本实现

标签:ace   int   space   通过   public   keyword   方法   abs   x11   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!