10.匿名内部类的应用场景

匿名内部类作为参数传递

package com.itheima_04;

public interface Animal {
    public abstract void eat();
}
package com.itheima_04;

public class Cat implements Animal {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

}
package com.itheima_04;

public class Dog implements Animal {

    @Override
    public void eat() {
        System.out.println("狗啃骨头");
    }

}
package com.itheima_04;

/*
 *  匿名内部类的应用场景:
 *          作为参数进行传递
 * 
 */
public class InnerDemo5 {
    public static void main(String[] args) {
//      method(new Dog());
//      method(new Cat());

        method(new Animal() {

            @Override
            public void eat() {
                System.out.println("猫吃鱼");
            }
        });
    }

    public static void method(Animal a) {
        a.eat();
    }
}

你可能感兴趣的:(10.匿名内部类的应用场景)