闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域。通过这个定义,可以看出内部类是面向对象的闭包,因为它不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥有一个指向此外围类对象的引用,在此作用域内,内部类有权操作所有的成员,包括private成员。
Java最引人争议的问题之一就是,人们认为Java应该包含某种类似指针的机制,以允许回调(callback)。通过回调,对象能够携带一些信息,这些信息允许它在稍后的某个时刻调用初始的对象。
如果回调是通过指针实现的,那么就只能寄希望于程序员不会误用该指针。然而,您应该已经了解到,Java更小心仔细,所以没有在语言中包括指针。
对于Java而言,通过内部类提供闭包的功能是优良的解决方案,它比指针更灵活、更安全。
// Using inner classes for callbacks
package innerclasses
import static net.mindview.util.Print.*;
interface Incrementable {
void increment();
}
// Very simple to just implement the interface
class Callee1 implements Incrementable {
private int i = 0;
public void increment() {
i++
print(i);
}
}
class MyIncrement {
public void increment() {print("Other operation");}
static void f(MyIncrement mi) {mi.increment();}
}
// If your class must implement increment() in
// some other way, you must use an inner class:
class Callee2 extends MyIncrement {
private int i=0;
public void increment() {
super.increment();
i++;
print(i);
}
private class Closure implements Incrementable {
public void increment() {
// Specify outer-class method, otherwise
// you'd get an infinite recursion
Callee2.this.increment();
}
}
Incrementable getCallbackReference() {
return new Closure();
}
}
class Caller {
private Incrementable callbackReference;
Caller(Incrementable cbh) {callbackReference = cbh;}
void go() {callbackReference.increment();}
}
public class Callbacks {
public static void main(String[] args) {
Callee1 c1 = new Calleel();
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 operation 1 1 2 Other operation 2 Other operation 3
内部类Closure实现了Incrementable,以提供一个返Callee2的“钩子”(hook)——而且是一个安全的钩子。无论谁获得此 Incrementable的引用,都只能调用increment(),除此之外没有其他功能(不像指针那样,允许您做很多事情)。
Caller的构造器需要一个Incrementable的引用作为参数(虽然可以在任意时刻捕获回调引用),然后在以后的某个时刻,Caller对象可以使用此引用回调Callee类。
回调的价值在于它的灵活性——可以在运行时动态地决定需要调用什么方法。
487

被折叠的 条评论
为什么被折叠?



