JS当中的一段实现“加法器”的代码。
function makeAdder(a) {
return function(b) {
return a + b;
};
}
var fiveAdder = makeAdder(5);
var tenAdder = makeAdder(10);
fiveAdder(6); // ?
tenAdder(7); // ?
显然,fiveAdder
是用来每次加5的加法器,而tenAdder
是用来每次加10的加法器。
一个很直接的问题便是,fiveAdder
的5和tenAdder
的10被存在哪里?
Inner class
之于class
,就好比是first class function(一等函数)之于函数。只不过这里是first class class(一等类)。你可以直接把一个类的实现返回出来,而不再是返回一个简单的数值。
来自Thinking in Java的一个例子:
interface Incrementable {
void increment();
}
class Callee1 implements Incrementable {
private int i = 0;
public void increment() {
i++;
System.out.println(i);
}
}
class MyIncremental {
public void increment() {
System.out.println("other operations");
}
static void f(MyIncremental mi) {
mi.increment();
}
}
class Callee2 extends MyIncremental implements Incrementable {
private int i = 0;
@Override
public void increment() {
super.increment();
i++;
System.out.println(i);
}
// The non-trivial part!
private class Closure implements Incrementable {
public void increment() {
Callee2.this.increment();
}
}
Incrementable getCallbackRef() {
return new Closure();
}
}
// The initiator of calling.
class Caller {
private Incrementable callbackRef;
Caller(Incrementable cbh) {
callbackRef = cbh;
}
void go() {
// ...... do lots of other things.
// at the end:
callbackRef.increment();
}
}
public class Callbacks {
public static void main(String... args) {
Callee1 c1 = new Callee1();
Callee2 c2 = new Callee2();
MyIncremental.f(c2);
Caller caller1 = new Caller(c1);
caller1.go();
caller1.go();
Caller caller2 = new Caller(c2);
caller2.go();
caller2.go();
}
}
网友评论