用惯了ajax,喜欢上了回调函数。所以写了个小例子实现了下java 中的回调
public interface CallBackInterface {
public void doCallBack(String str);
}
public class CallBackMethod {
public void doCallBack(String str,CallBackInterface call){
//加工传入参数并得到最终结果res
String res = str + "callBack";
//将res传入接口
call.doCallBack(res);
}
}
public class Test {
public static void main(String[] args) {
new CallBackMethod().doCallBack("show ", new CallBackInterface(){
@Override
public void doCallBack(String str) {
// TODO Auto-generated method stub
System.out.println(str);
}
});
}
}
通过回调函数,我们拿到了加工后的值。