特点:
匿名内部类只能被使用一次,(如果个类只使用一次可以通过匿名类来实现
)它就是用来实体化一个抽象类或者接口的,直接在方法里面实现抽象方法。
举例:
这个是实现抽象类
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();
}
}
最后
以上就是开放小懒虫最近收集整理的关于Java基础:匿名内部类的全部内容,更多相关Java基础内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复