分类: Java
2014-06-15 10:50:00
Define an interface, and implement it in the class that will receive the callback.
Have attention to the multi-threading in your case.
Code example from %E5%A4%9A%E7%BA%BF%E7%A8%8B/java-callback.html
interface CallBack { void methodToCallBack(); } class CallBackImpl implements CallBack { public void methodToCallBack() { System.out.println("I've been called back"); } } class Caller { public void register(CallBack callback) { callback.methodToCallBack(); } public static void main(String[] args) { Caller caller = new Caller(); CallBack callBack = new CallBackImpl(); caller.register(callBack); } }