ITPub博客

首页 > 应用开发 > Java > java 回调函数示例

java 回调函数示例

原创 Java 作者:hgs19921112 时间:2018-08-20 10:25:57 0 删除 编辑
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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-11-22

  • 博文量
    106
  • 访问量
    186398