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