zoukankan      html  css  js  c++  java
  • Java回调函数

     回调就是调用方持有被调用方的一个引用,而调用方接口的实现类作为被调用方的参数,被调用方返回来调用调用方的方法 
     

    接口CallBack

    public interface CallBack {

    /**回调方法*/
    public void execute(Object ... objects);
    }

    调用方Local

    public class Local implements CallBack ,Runnable{
    private Remote remote;//被调用方作为调用方的一个引用
    private String msg;

    public Local(Remote remote, String msg) {
    super();
    this.remote = remote;
    this.msg = msg;
    }

    @Override
    public void execute(Object... objects) {

    //返回的消息
    System.out.println(objects[0]);

    System.out.println(Thread.currentThread().getName());
    Thread.interrupted();
    }

    public void sendMessage() {
    System.out.println(Thread.currentThread().getName());
    Thread thread = new Thread(this);
    thread.start();
    System.out.println("发送消息");
    }
    public static void main(String[] args)
    {
    Local local = new Local(new Remote(),"Hello");

    local.sendMessage();
    }
    @Override
    public void run() {
    remote.executeMessage(msg, this);
    }

    }

    被调用方Remote

    public class Remote {
    public void executeMessage(String msg,CallBack callBack){

    System.out.println(msg);
    System.out.println("接收到了Local传过来的命令");
    callBack.execute(new String[]{"Hello Local"});
    }
    }

  • 相关阅读:
    asp .net 页面回车触发button 按钮事件
    Asp.netPost&Get转
    2012.8.16近期总结,1模态窗口回传重新被弹出2,清空缓存 3,
    面试感言
    2012.6.27近期总结,1.sql字符串转换(cast,CONVERT )调用wcf服务,关闭模态窗口刷新付页面
    self
    空指针
    枚举和结构体
    typedef
    指针
  • 原文地址:https://www.cnblogs.com/anxue/p/4724628.html
Copyright © 2011-2022 走看看