首先记住一句两个人的对话:
A to B:i call you if I want you help doing sth;
B to A: ok ,I doing sth;
B to A: if I done, I will call you back.
这就是回调函数----------
回调:我想请你帮我个忙,你做好了叫我。好,我做好了叫你。
非回调(伪码): do (I want to you help me do sth),
while( finished()? ){
if(finished() == true) break;
else{ wait(3000) }
}
do others().
B:
public class B implements A.Callback {
@Override
public void tellAdone() {
System.out.println("我做好了,你可以放心啦!");
}
public void Requsetsb(){
System.out.println("i need you ,help me !");
A a =new A();
a.Accept(this);
}
public static void main(String[] args) {
B b = new B();
b.Requsetsb();
}
}
public class A {
public interface Callback{
void tellAdone();
}
public void Accept(Callback callback){
System.out.println("ok,我做好了叫你");
dosth();
done_sth();
callback.tellAdone();
}
public void dosth(){
System.out.println("我正在努力做事情");
}
public void done_sth(){
System.out.println("我做好了");
}
}
输出:
i need you ,help me !
ok,我做好了叫你
我正在努力做事情
我做好了
我做好了,你可以放心啦!