紅知博客

          知其然,亦知其所以然
          posts - 32, comments - 35, trackbacks - 0, articles - 0

          java中的閉包與回調(diào) (轉)

          Posted on 2007-09-17 14:44 紅子 閱讀(1256) 評論(1)  編輯  收藏 所屬分類: JAVAJ2EE 綜合

          java中的閉包與回調(diào)


          閉包是一個可調(diào)用的對象,它記錄了一些信息,這些信息來自于創(chuàng)建他的作用域,用過這個定義 可以看出內(nèi)部類是面向對象的閉包 因為他不僅包含外圍類對象的信息 還自動擁有一個指向此外圍類對象的引用 在此作用域內(nèi) 內(nèi)部類有權操作所有的成員 包括private成員;

          interface Incrementable
          {
           void increment();
          }

           

          class Callee1 implements Incrementable
          {
           private int i=0;
           public void increment()
           {
            i++;
            System.out.println(i);
           }
          }

          class MyIncrement
          {
           void increment()
           {
            System.out.println("other increment");
           }
           static void f(MyIncrement mi)
           {
            mi.increment();
           }
          }

          class Callee2 extends MyIncrement
          {
           private int i=0;
           private void incr()
           {
            i++;
            System.out.println(i);
           }
           private class Closure implements Incrementable
           {
            public void increment()
            {
             incr();
            }
           }
           Incrementable getCallbackReference()
           {
            return new Closure();
           }
          }

          class Caller
           {
            private Incrementable callbackRefference;
            Caller(Incrementable cbh)
            {
             callbackRefference = cbh;
            }
            void go()
            {
             callbackRefference.increment();
            }
           }

          public class Callbacks
          {
           public  static void main(String [] args)
           {
            Callee1 c1=new Callee1();
            Callee2 c2=new Callee2();
            MyIncrement.f(c2);
            Caller caller1 =new  Caller(c1);
            Caller caller2=new Caller(c2.getCallbackReference());
            caller1.go();
            caller1.go();
            caller2.go();
            caller2.go();
           }
          }

          輸出

          other increment
          1
          2
          1
          2

          Callee2 繼承字MyIncrement 后者已經(jīng)有一個不同的increment()方法 并且與Incrementable接口期望的increment()方法完全不相關 所以如果Callee2繼承了MyIncrement 就不能為了Incrementable的用途而覆蓋increment()方法 于是這能使用內(nèi)部類獨立的實現(xiàn)Incrementable

          內(nèi)部類Closure實現(xiàn)了Incrementable 一提供一個放回Caller2的鉤子 而且是一個安全的鉤子 無論誰獲得此Incrementbale的引用 都只能調(diào)用increment() 除此之外沒有其他功能

          Feedback

          # re: java中的閉包與回調(diào) (轉)  回復  更多評論   

          2007-09-28 15:32 by 千里冰封
          現(xiàn)在還沒有實現(xiàn)閉包吧
          我要啦免费统计
          主站蜘蛛池模板: 年辖:市辖区| 漳平市| 兰溪市| 九江县| 太原市| 洛扎县| 富锦市| 锦屏县| 禹州市| 汨罗市| 西充县| 小金县| 大关县| 汕尾市| 志丹县| 荔波县| 佛冈县| 邵武市| 肃北| 灵川县| 宿迁市| 尼勒克县| 阳山县| 屯留县| 新龙县| 井陉县| 射洪县| 青冈县| 双峰县| 伊宁市| 枞阳县| 湖北省| 白朗县| 乌海市| 社旗县| 江安县| 凌云县| 越西县| 沙坪坝区| 蒙山县| 荆门市|