interface Incrementable{
void increment();
}class Callee1 implements Incrementable{
private int i=0;
public void increment(){
i++;
System.out.println(i);
}
}class Myincrement{
public void increment(){
System.out.println("other operation");
}
public 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 callbackReference;
Caller(Incrementable cbh){
callbackReference=cbh;
}
void go(){
callbackReference.increment();
}
}public class Callbacks {
public Callbacks() {
}
public static void main(String[] args) {
Callee1 c1 = new Callee1();
Callee2 c2=new Callee2();
//此处调用Myincrement类的f函数,输入other operation
Myincrement.f(c2); 
Caller caller1=new Caller(c1);
Caller caller2=new Caller(c2.getCallbackReference());
//caller1.go()调用的是callee1.increment(),所以输出为1。
caller1.go();
//此处继续调用callee1.increment(),所以输出为2。
caller1.go();
//caller2.go()调用的是caller2.incr(),所以输出1。
caller2.go();
//继续调用callee2.incr(),所以输出为2.
caller2.go();
}
}
我主要对main进行了调用与输出的说明,只有细致的分析才能真正明白程序,所以,希望大家能帮助我,检查我分析的对不对,哪有问题,请直接批评。