您的位置:首页 > 移动开发 > Android开发

对Android回调的理解

2016-03-03 18:04 417 查看
场景:

C问S一个问题,但S不能马上给他答案,于是S决定想到答案再告诉C。S约定通过打电话的方式告诉C,C需要留下电话给S。

S将答案告诉C是回调,联系方式是S和C先约定好的接口,C留联系方式给S是注册过程。

代码:

//声明一个接口 ,约定好工具
public interface Tool {
void answer();
}

public class C {
private Tool tool;
public void setCallBack(Tool tool) {
this.tool = tool;
}
public void ask() {
tool.answer();
}
}

public class S {
public static void main(String[] args) {
C c = new C();
//c使用setCallBack()留联系方式,留的内容是Tool
c.setCallBack(new Tool() {
public void answer() {
System.out.println("answer the question.");
}
});
c.ask();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: