随笔 - 63  文章 - 0  trackbacks - 0
<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个时候反过来调用C中的某个函数B,对于C来说,这个B便叫做回调函数。

例:  
  1.class   A,class   B  
  2.class   A实现接口operate  
  3.class   B拥有一个参数为operate接口类型的函数test(operate   o)  
  4.class   A运行时调用class   B中test函数,以自身传入参数  
  5.class   B已取得A,就可以随时回调A所实现的operate接口中的方法

举个例子来说:

public interface InterestingEvent
{

    // This is just a regular method so it can return something or

    // take arguments if you like.

    public void interestingEvent ();

}






public class EventNotifier
{

    private InterestingEvent ie;

    private boolean somethingHappened;

    public EventNotifier (InterestingEvent event)
    {

        // Save the event object for later use.

        ie = event;

        // Nothing to report yet.

        somethingHappened = false;

    }

    // 

    public void doWork ()
    {

        // Check the predicate, which is set elsewhere.

        if (somethingHappened)
        {

            // Signal the even by invoking the interface's method.

            ie.interestingEvent ();

        }

        //
    }

    //

}



public class CallMe implements InterestingEvent
{

    private EventNotifier en;

    public CallMe ()
    {

        // Create the event notifier and pass ourself to it.

        en = new EventNotifier (this);

    }

    // Define the actual handler for the event.

    public void interestingEvent ()
    {

        // Wow!  Something really interesting must have occurred!

        // Do something

    }

    //

}






















posted on 2009-01-20 08:51 lanxin1020 阅读(205) 评论(0)  编辑  收藏 所属分类: j2se

只有注册用户登录后才能发表评论。


网站导航: