package callBack7;
public interface CallBack {
// 回调方法,用于返回结果
public void solve(String question);
}
public interface CallBack {
// 回调方法,用于返回结果
public void solve(String question);
}
package callBack7;
//客户端
public class Client implements CallBack {
Service service;
public Client(Service service) {
super();
this.service = service;
}
public void ask(String question){
// 由于自己解决不了,所以找客服帮忙
service.getResult(this, question);
}
@Override
public void solve(String question) {
System.out.println("客服回答:"+ question);
}
}
//客户端
public class Client implements CallBack {
Service service;
public Client(Service service) {
super();
this.service = service;
}
public void ask(String question){
// 由于自己解决不了,所以找客服帮忙
service.getResult(this, question);
}
@Override
public void solve(String question) {
System.out.println("客服回答:"+ question);
}
}
package callBack7;
//服务端
public class Service {
public void getResult(CallBack callBack , String question){
System.out.println("客户的问题是:" + question);
for (int i = 0; i < 20; i++) {
// 模拟客服在查询余票
System.out.println("余票查询中。。。");
}
String result = "你好,往广州的D1234号动车还有----3张余票";
//调用的是该接口的实现方法(即Client中的solve()
callBack.solve(result);
}
}
//服务端
public class Service {
public void getResult(CallBack callBack , String question){
System.out.println("客户的问题是:" + question);
for (int i = 0; i < 20; i++) {
// 模拟客服在查询余票
System.out.println("余票查询中。。。");
}
String result = "你好,往广州的D1234号动车还有----3张余票";
//调用的是该接口的实现方法(即Client中的solve()
callBack.solve(result);
}
}
package callBack7;
public class Test {
//测试类
public static void main(String[] args) {
Service service = new Service();
Client client = new Client(service);
client.ask("请问今天到广州的票还有么");
}
}
public class Test {
//测试类
public static void main(String[] args) {
Service service = new Service();
Client client = new Client(service);
client.ask("请问今天到广州的票还有么");
}
}