将参数传递给计时器任务(Java)
我有一个循环中的计时器任务。我想把循环中的数字传递到时间任务中 可能吗 我的代码:将参数传递给计时器任务(Java),java,timer,Java,Timer,我有一个循环中的计时器任务。我想把循环中的数字传递到时间任务中 可能吗 我的代码: ... int i = 0; while (i < array.size){ Timer timer = new Timer(); timer.schedule(new RegrowCornAnimate(), 0, 1000); i++ } ... class RegrowCornAnimate extends TimerTask { public void run() { /
...
int i = 0;
while (i < array.size){
Timer timer = new Timer();
timer.schedule(new RegrowCornAnimate(), 0, 1000);
i++
}
...
class RegrowCornAnimate extends TimerTask {
public void run() {
//Do stuff
}
}
。。。
int i=0;
while(i
如何更改它以便在TimerTask类中使用I
-在每个TimerTask中,都会知道它是在/in/from下创建的i
。类RegrowCornAnimate扩展了TimerTask{
class RegrowCornAnimate extends TimerTask {
private final int serial;
RegrowCornAnimate ( int serial )
{
this.serial = serial;
}
public void run() {
//Do stuff
}
}
...
int i = 0;
while (i < array.size){
Timer timer = new Timer();
timer.schedule(new RegrowCornAnimate( i ), 0, 1000);
i++;
}
...
私人最终int系列;
重新生长CornAnimate(整型序列)
{
this.serial=serial;
}
公开募捐{
//做事
}
}
...
int i=0;
while(i
给重新生长CornAnimate
类一个构造函数,该构造函数接受一个int
,并将其存储在一个字段中。创建构造函数时将i
传递给构造函数。在RegrowCornAnimate
中创建一个构造函数,获取要使用的参数,然后将其作为成员存储在类中
当调用重新生长CornAnimate.run时,读取值。请参见
. 此示例在每次运行时打印一个数字 这正是我想要的谢谢,我不到两周前就知道了,我怎么会忘记啊@史提芬。没问题。熟能生巧!你应该考虑充实你的答案,最好是把相关内容从链接站点中收录出来。仅链接的答案可以解释为垃圾邮件/非答案(因为它们很容易受到链接腐烂的影响)。