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

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          所謂回調(diào),就是客戶程序C調(diào)用服務程序S中的某個函數(shù)A,然后S又在某個時候反過來調(diào)用C中的某個函數(shù)B,對于C來說,這個B便叫做回調(diào)函數(shù)。

          例:  
            1.class   A,class   B  
            2.class   A實現(xiàn)接口operate  
            3.class   B擁有一個參數(shù)為operate接口類型的函數(shù)test(operate   o)  
            4.class   A運行時調(diào)用class   B中test函數(shù),以自身傳入?yún)?shù)  
            5.class   B已取得A,就可以隨時回調(diào)A所實現(xiàn)的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 閱讀(220) 評論(0)  編輯  收藏 所屬分類: j2se
          主站蜘蛛池模板: 南部县| 遵化市| 渝中区| 柳江县| 南漳县| 庄河市| 洮南市| 成都市| 双牌县| 宕昌县| 宜章县| 临澧县| 靖西县| 郸城县| 黑河市| 阿拉善左旗| 城固县| 兴业县| 广饶县| 天津市| 彭水| 洛隆县| 微山县| 南开区| 宜宾市| 平陆县| 简阳市| 灵宝市| 巴林左旗| 新和县| 绥化市| 洞口县| 太仆寺旗| 福安市| 晴隆县| 金塔县| 湖口县| 昌都县| 班戈县| 息烽县| 夹江县|