使用接口,也就是使用多态来实现回调函数
public interface ICallback {
public void func();
}
public class ClassWithCallbackFunction implements ICallback{
public ClassWithCallbackFunction() {
}
public void func(){
System.out.println("cccccccccccccccccc");
}
}
public class Caller {
ICallback callback;
public void doCallback() {
callback.func();
}
public void setCallback(ICallback callback) {
this.callback = callback;
}
}
public class MainClass {
public MainClass() {
}
public static void main(String[] args) {
Caller caller = new Caller();
caller.setCallback(new ClassWithCallbackFunction() {
public void func() {
System.out.println("aaaaaaaaaa");
}
});
caller.doCallback();
}
}