匿名内部类
2019-03-17 / JAVA / 919 次围观 / 0 次吐槽 /抽象类的匿名内部类
接口的匿名内部类
new 抽象类 或者接口,都必须实现里面的所有方法
public static void main(String[] args) { // TODO Auto-generated method stub Animal dog = new Dog(); dog.eat(); /*//这种写法只能在结尾调用一个方法。 new Animal(){ @Override public void eat() { // TODO Auto-generated method stub System.out.println("猫吃鱼"); } }.eat(); */ Animal cat = new Animal(){ @Override public void eat() { // TODO Auto-generated method stub System.out.println("猫吃鱼"); } }; cat.eat(); } } abstract class Animal{ public abstract void eat(); } class Dog extends Animal{ @Override public void eat() { // TODO Auto-generated method stub System.out.println("狗啃骨头"); } }
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.