Fla*_*dun 6 java anonymous-class
这是代码:
timer.schedule(new TimerTask()
{
public void run()
{
synchronized(this)
{
try
{
// System.out.println(" ITERATION = ");
pachubeCLI.update(78164);
}
catch (PachubeException e)
{
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}, 0, 5*1000);
Run Code Online (Sandbox Code Playgroud)
我可以说,代码基本上用于使用Timer类的对象来安排操作.schedule根据eclipse 传递给方法的参数是(TimerTask task,long delay, long period).但是看一下这段代码,整个代码块作为第一个参数而不是对TimerTask类的引用传递.我以前从未见过这样的方法.到底发生了什么?
一些背景:该对象的schedule方法Timer用于定期更新Xively(以前的COSM(以前的pachube))上的Feed.
此外,我不知道哪个标签描述了这里发生的事情.如果你这样做,请添加或发表评论.
你所看到的被称为"匿名内部阶级".
请参阅此处的javadoc:http: //docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
这个问题: Java中如何使用匿名(内部)类?.
而且,为了记录,您在问题中使用的两个标签都是合适的.
| 归档时间: |
|
| 查看次数: |
430 次 |
| 最近记录: |