标签:ctc interface alt nts 回调 sar object mic ret
回调就是就调用方在调用被调用方时留给对方一个小尾巴,在被调方需要的时候通过这个小尾巴把消息通知给调用方实现例子如下,一如既往设计一个顶层接口
/**
*/
public interface CallBack {
public void callback(Object object);
}
调用对象
package callback;
/**
*/
public class BusARequest implements CallBack {
public void execute() {
//调用被调方
BusResponse rep = new BusResponse();
//将自己同时传给被调用方
rep.call(this, 1);
}
@Override
//实现的回调方法
public void callback(Object object) {
getResult((int)object);
}
public int getResult(int para) {
System.out.println("参数:"+para);
return para;
}
}
被调用对象进行回调
package callback;
/**
*/
public class BusResponse {
public void call(CallBack callback,Object object) {
System.out.println("进入操作");
System.out.println("回调对象类型:"+callback.getClass().getSimpleName());
callback.callback(object);
}
}
测试示例
package callback;
public class CallBackMain {
public static void main(String[] args) {
BusARequest objectA = new BusARequest();
BusBRequest objectB = new BusBRequest();
BusCRequest objectC = new BusCRequest();
objectA.execute();
objectB.execute();
objectC.execute();
}
}
调用结果
标签:ctc interface alt nts 回调 sar object mic ret
原文地址:https://blog.51cto.com/4890631/2487468