java通过内部类实现闭包

来源:互联网 发布:软件开发入门书籍 编辑:程序博客网 时间:2024/06/04 19:54

闭包是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作域。
代码:

interface Incrementable{    void increment();}//接口的普通实现class Callee1 implements Incrementable{    private int i=0;    public void increment(){        i++;        System.out.println(i);    }}//定义了一个类,与Incrementable接口有同名的方法class Myincrement{    public void increment(){        System.out.println("other operation");    }    static void f(Myincrement mi){        mi.increment();    }}//因为继承自Myincrement,所以如果想实现Incrementable接口,需要使用内部类class Callee2 extends Myincrement{    private int i=0;    public void increment(){        super.increment();        i++;        System.out.println(i);    }    private class Closure implements Incrementable{        public void increment(){            Callee2.this.increment();        }    }    Incrementable getCallbackReference(){        return new Closure();    }}class Caller{    private Incrementable callbackReference;    Caller(Incrementable cbn){        callbackReference=cbn;    }    void go(){        callbackReference.increment();    }}public class Callbacks {    public static void main(String[] args){        Callee1 c1=new Callee1();        Callee2 c2=new Callee2();        Myincrement.f(c2);//这里f调用的是c2的increment(),输出other operation 1        Caller caller1=new Caller(c1);        Caller caller2=new Caller(c2.getCallbackReference());        caller1.go();//1        caller1.go();//2        caller2.go();//other operation 2        caller2.go();//other operation 3    }}

总结:
1)如果Callee2继承了MyIncrement,就不能为了Incrementable的用途而覆盖increment()方法,于是只能使用内部类独立的实现Incrementable。
2)在Callee2中除了getCallbackReference()以外,其他的成员都是private。要想建立与外部世界的任何连接,interface Incrementable都是必须的。
3)内部类Closure实现了Incrementable,以提供一个返回Callee2的“钩子”(hook)——而且是一个安全的钩子。
**4)**Caller构造器需要一个Incrementable的引用作为参数,然后在以后的某个时刻,Caller对象可以使用此引用回调Callee类。

原创粉丝点击