关键词: Java 回调函数 CallBack
Java中的"回调函数"一般是用接口来实现的,如
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.");}
});
}
}
public interface ICallBack(){
void postExec();
}
public class FooBar..{
private ICallBack callBack;
public void setCallBack(ICallBack callBack){
this.callBack=callBack;
}
public void doSth(){
....
callBack.postExec();
}
..
}
也可以使用Observer模式来实现类似的功能
<转载>
转载于:https://my.oschina.net/iqoFil/blog/221558
来源:https://blog.csdn.net/cichaojiao4477/article/details/100984722