紅知博客

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

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

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

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


          閉包是一個可調(diào)用的對象,它記錄了一些信息,這些信息來自于創(chuàng)建他的作用域,用過這個定義 可以看出內(nèi)部類是面向?qū)ο蟮拈]包 因為他不僅包含外圍類對象的信息 還自動擁有一個指向此外圍類對象的引用 在此作用域內(nèi) 內(nèi)部類有權(quán)操作所有的成員 包括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()方法完全不相關(guān) 所以如果Callee2繼承了MyIncrement 就不能為了Incrementable的用途而覆蓋increment()方法 于是這能使用內(nèi)部類獨(dú)立的實(shí)現(xiàn)Incrementable

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

          Feedback

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

          2007-09-28 15:32 by 千里冰封
          現(xiàn)在還沒有實(shí)現(xiàn)閉包吧
          我要啦免费统计
          主站蜘蛛池模板: 浪卡子县| 平乡县| 山东| 广东省| 昭平县| 盘山县| 驻马店市| 乐至县| 黄骅市| 大石桥市| 来安县| 通许县| 宁化县| 秭归县| 集安市| 龙井市| 德惠市| 澳门| 崇左市| 共和县| 攀枝花市| 郁南县| 孝义市| 柳江县| 平和县| 会泽县| 南平市| 靖远县| 政和县| 三穗县| 托里县| 北宁市| 株洲市| 当雄县| 阿拉尔市| 长岭县| 兰西县| 莱阳市| 景德镇市| 化州市| 汕尾市|