首页 > 应用开发 > Java > java 回调函数示例
public class Callback {
static A a= null;
public static void main(String[] args) {
int x = 100;
a = new A(x,new B() {
@Override
void otherAuth() {
//2.调用自己的函数
int num = this.Changethenum(a.a);
//3. 回调A的callBack
a.callBack(num);
}
});
//1.A 调自己的callB
a.callB();
}
}
abstract class B{
public B( ) {
}
abstract void otherAuth();
public int Changethenum(int num) {
return (int) Math.pow(num, 3);
}
}
class A {
int a ;
B b = null;
A(int a , B b ){
this.a = a ;
this.b = b;
}
void callB(){
b.otherAuth();
}
void callBack(int i ) {
System.out.println("The number changed : "+i);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31506529/viewspace-2212326/ ,如需转载,请注明出处,否则将追究法律责任。