package 内部类;//实现接口
interface Incrementable{
void increment();
}class Callee1 implements Incrementable{
private int i=0;
public void increment(){
i++;
System.out.println("Callee1.i="+i);
}
}
//类的继承class MyIncrement{
void increment(){
System.out.println("other operation");
}
static void f(MyIncrement mi){
mi.increment();
}
}class Callee2 extends MyIncrement{
private int i=0;
/* void increment(){
System.out.println("other operation------------向上转型-》动态绑定");
}*/
private void incr(){
i++;
System.out.println("Callee2.i="+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 CallBack { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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();
System.out.println();
caller2.go();
caller2.go();
System.out.println();
caller1.go();
caller1.go();
System.out.println();
caller2.go();
caller2.go();
}}
初学者一名,上面代码看了半天还是没明白什么是回调,起什么作用。哪位大哥能解释一下,不甚感激````
interface Incrementable{
void increment();
}class Callee1 implements Incrementable{
private int i=0;
public void increment(){
i++;
System.out.println("Callee1.i="+i);
}
}
//类的继承class MyIncrement{
void increment(){
System.out.println("other operation");
}
static void f(MyIncrement mi){
mi.increment();
}
}class Callee2 extends MyIncrement{
private int i=0;
/* void increment(){
System.out.println("other operation------------向上转型-》动态绑定");
}*/
private void incr(){
i++;
System.out.println("Callee2.i="+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 CallBack { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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();
System.out.println();
caller2.go();
caller2.go();
System.out.println();
caller1.go();
caller1.go();
System.out.println();
caller2.go();
caller2.go();
}}
初学者一名,上面代码看了半天还是没明白什么是回调,起什么作用。哪位大哥能解释一下,不甚感激````
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货