1,声明一个回调函数接口
public interface CallBack {
String execute();
}
2,创建业务实现
public class BusinessServer {
public static String test(String name){
System.out.println("BusinessServer类test():MyTimer 接收到了name:" + name);
System.out.println("BusinessServer类test():正在处理.." );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return "[ " +name+ " ] Time success";
}
}
3,调用回调函数类
public class Test {
public String testTime(CallBack callBack){
long begin = System.currentTimeMillis();
String res = callBack.execute();
long end = System.currentTimeMillis();
System.out.println("Test类testTime():花费时间:"+ (end - begin) + ",获取到res: " + res);
return res;
}
public static void main(String[] args) {
Test test = new Test();
System.out.println("Test类main() start !");
String myName = "小风";
String result = test.testTime(new CallBack() {
@Override
public String execute() {
return BusinessServer.test(myName);
}
});
System.out.println("Test类main() end ! 结果是: " + result);
}
}
