我有一个定义如下的函数:
public Object test(int arg0){
final Time tt = new Timer();
tt.schedule(new TimerTask() {
public void run(){
Object j = anotherMethod(arg0);
//Do some checks on object j and if it fits, return this j
//I want to be able to return this object j that I get from another method
}
} , interval);
}
基本上,我想在TimerTask中的run方法中调用一个函数并进行一些检查,如果函数返回我想要的函数,我想在测试中返回它(上面的方法运行方法)。我怎么能这样做?
答案 0 :(得分:1)
我认为最好使用TimerTask类是让它扩展Observable类并创建一个方法:
public void update(){
setChanged();
notifyObervers();
}
您将调用方法内部的函数,该函数检测是否该做某事。包含检查内容的方法的类,它将被注册为观察者,它将实现Interface Observer的方法。你想做什么并不尊重SOLID范式。我希望这个回复对你有用。