1.定义接口
public interface ContactInterface {
public void callBackByTel(String answer);
}
2.注册接口
public class Me {
public static void main(String[] args){
Me me = new Me();
me.hasQuestion();
}
private void hasQuestion(){
//现在有问题想不出来答案,想去问你
You you = new You();
you.setCallBack("蜗牛", "某道题答案是什么?", new ContactInterface() {
@Override
public void callBackByTel(String answer) {
System.out.println("我说:嗯,好的,我收到答案了:"+answer+",谢谢");
}
});
//你接到电话,起床开始思考问题
new Thread(you).start();
}
3.接口调用
public class You implements Runnable{
private String who;
private ContactInterface callBack;
public You() {
// TODO Auto-generated constructor stub
}
//调用此方法就表示有人联系你了,注册到你这来
public void setCallBack(String who,String question,ContactInterface callBack) {
this.who = who;
System.out.println("你说:当前联系到我的人是"+who+",问题是"+question);
this.callBack =callBack;
}
public void handleThings(){
//假如你现在正在想问题的答案,需要一点时间
for(int i=0;i<100000;i++){
if(i == 0){
System.out.println("你正在思考问题.....");
}
}
String answer = "答案是A";
//想到问题的办法了
System.out.println("你说:想到答案了,准备打回去给"+who+"告诉他答案");
callBack.callBackByTel(answer);
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
Thread.sleep(1000);
handleThings();
} catch (Exception e) {
e.printStackTrace();
}
}