public class Caller {
public MyCallInterface mc;
public void setCallfuc(MyCallInterface mc){
this.mc= mc;
}
public void call(){
this.mc.method();
}
}
public interface MyCallInterface {
public void method();
}
public class B implements MyCallInterface {
public void method() {
System.out.println("谁来调用我啊。好寂寞!!");
}
public static void main(String args[]) {
Caller call = new Caller();
call.setCallfuc(new B());
call.call();
}
}
//声明一个接口
public interface ICallBack {
void postExec();
}
//另外一个类有方法里面有个参数以是这个接口类型的
public class FooBar {
private ICallBack callBack;
public void setCallBack(ICallBack callBack) {
this.callBack = callBack;
}
public void doSth() {
callBack.postExec();
}
}
----------------------------------------
回调的实现
public class Test {
public static void main(String[] args) {
FooBar foo = new FooBar();
foo.setCallBack(new ICallBack() {
public void postExec() {
System.out.println("method executed.");
}
});
foo.doSth();//调用函数
}
}
public class TestObject {
/**
* 一个用来被测试的方法,进行了一个比较耗时的循环
*/
public static void testMethod(){
for ( int i= 0 ; i< 100000000 ; i++){
}
}
/**
* 一个简单的测试方法执行时间的方法
*/
public void testTime(){
long begin = System.currentTimeMillis(); //测试起始时间
testMethod(); //测试方法
long end = System.currentTimeMillis(); //测试结束时间
System.out.println("[use time]:" + (end - begin)); //打印使用时间
}
public static void main(String[] args) {
TestObject test=new TestObject();
test.testTime();
}
}
public interface CallBack {
//执行回调操作的方法
void execute();
}
public class Tools {
/**
* 测试函数使用时间,通过定义CallBack接口的execute方法
* @param callBack
*/
public void testTime(CallBack callBack) {
long begin = System.currentTimeMillis(); //测试起始时间
callBack.execute(); ///进行回调操作
long end = System.currentTimeMillis(); //测试结束时间
System.out.println("[use time]:" + (end - begin)); //打印使用时间
}
public static void main(String[] args) {
Tools tool = new Tools();
tool.testTime(new CallBack(){
//定义execute方法
public void execute(){
//这里可以加放一个或多个要测试运行时间的方法
TestObject.testMethod();
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有