您的位置:首页 > 编程语言 > Java开发

Java 一个简单的回调模式

2014-06-29 00:00 120 查看
摘要: 就像我们给按钮添加监听器一样的效果

我们需要定义一个接口:

package linving.test;

public interface MyTestInterface {
public int getUpdate(int i);

}

接下来给这个接口的实现更新

package linving.test.update;

import linving.test.MyTestInterface;

public class Update {
public void UpdateSometing(MyTestInterface callback){
int i = 0;
while(true){
i++;
callback.getUpdate(i);
}

}
}

主函数:

package linving.test.main;

import linving.test.MyTestInterface;
import linving.test.update.Update;

public class TestMain {

public static void main(String[] args) {
// TODO Auto-generated method stub

MyTestInterface callback = new MyTestInterface() {

@Override
public int getUpdate(int i) {
// TODO Auto-generated method stub
System.out.println(i);
return i;
}
};
Update u = new Update();
u.UpdateSometing(callback);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: