Topic: 关于回调函数

  Print this page

1.关于回调函数 Copy to clipboard
Posted by: kjjx
Posted on: 2004-05-14 11:06

请问各位同行, 那儿有java回调函数的资料.谢了!

2.Re:关于回调函数 [Re: kjjx] Copy to clipboard
Posted by: Jove
Posted on: 2004-05-14 17:54

Java中的"回调函数"一般是用接口来实现的,如

public class Test{
   public static void main(String[] args){
     FooBar foo=new FooBar();
     foo.setCallBack(new ICallBack(){
        public void postExec(){System.out.println("method executed.");}
     });
   }
}

public interface ICallBack(){
  void postExec();
}
public class FooBar..{
  private ICallBack callBack;
  public void setCallBack(ICallBack callBack){
    this.callBack=callBack;
  }
  public void doSth(){
     ....
     callBack.postExec();
  }
  ..
}

也可以使用Observer模式来实现类似的功能


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923