天气:多云 风力:微风
- 回调
- 把实现回调接口的类对象传递到方法里面再利用此对象执行操作
- 异步,即在在方法里启动另一个线程来执行操作,或在操作方法里启动线程
public interface CallBack {
void solve(String text);
}
public class SolveEntity implements CallBack {
@Override
public void solve(String text) {
new Thread(() -> {
try {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
System.out.println(str + text);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
public class RequestEntity {
public void solve(String text, CallBack callBack){
callBack.solve(text);
System.out.println(text);
}
}
public static void main(String[] args) {
SolveEntity solveEntity = new SolveEntity();
RequestEntity requestEntity = new RequestEntity();
requestEntity.solve("requestEntity message", solveEntity);
}
}
网友评论