代碼如下:
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());
}
}
運行結果: