Java通过接口实现匿名类的实例代码

时间:2021-08-23 10:29:00

代码如下:


package com.chase.test;

 

/**
 * 通过接口实现匿名类的实例
 * 
 * @author Chase
 * 
 * @date 2013-10-18 下午04:28:17 
 *
 * @version V1.0
 */
interface I1 {
    void print();
    void eat();
}

public class AnonymousClass {

    public static String excute(I1 c){
        System.err.println("excute方法打印...");
        c.print();
        c.eat();
        return "excute()返回的值...";
    }

    public String testExcute() {
        return excute(new I1(){
                public void print() {
                    System.out.println("调用匿名内打印...");
                }

                public void eat() {
                    System.out.println("今天的饭菜好好吃...");
                }
            }) ;
        }

    public static void main(String[] args) {
        AnonymousClass entity = new AnonymousClass();
        System.out.println(entity.testExcute());
    }
}