匿名类如何具有参数? [英] How can an anonymous class have arguments?
本文介绍了匿名类如何具有参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不是一个java人,但我继承了一些代码,我需要补丁。我把源代码转换成netbeans,我得到的错误:Anonymous类实现接口;
执行者背景= Executors.newSingleThreadExecutor();
Runnable mylookupThread = new Runnable(FilePath,SearchIndex)
{
public void run()
{MainWindow.this.processFile(this.val $ FilePath);
Thread t = new Thread(new lookupThread(MainWindow.arrFile,true,false,this.val $ SearchIndex));
t.setName(Lookup);
t.setPriority(10);
t.start();
}
};
background.execute(mylookupThread);
Executors statusThread = Executors.newSingleThreadExecutor();
Runnable myStatusThread = new Runnable()
{
public void run()
{MainWindow.this.updateStatus();
}
};
statusThread.execute(myStatusThread);
错误在第二行弹出。帮助?
解决方案
使 mylookupThread
单独的类,并传递给 Executor
:
class LookupTask implements Runnable {
private final String filePath,searchIndex;
LookupTask(String filePath,String searchIndex){
this.filePath = filePath;
this.searchIndex = searchIndex;
}
public void run(){...}
}
...
background.execute(new LookupTask(filePath,searchIndex) );
其他方法是使 filePath,searchIndex
final:
final String filePath = ...
final String searchIndex = ...
执行器background = Executors.newSingleThreadExecutor();
Runnable mylookupThread = new Runnable(){
public void run(){MainWindow.this.processFile(filePath);
Thread t = new Thread(new lookupThread(MainWindow.arrFile,true,false,searchIndex));
t.setName(Lookup);
t.setPriority(10);
t.start();
}
};
background.execute(mylookupThread);
I'm not a java guy but I've inherited some code I need to patch up. I pulled the source into netbeans and I'm getting the error: Anonymous class implements interface; cannot have arguments.
Here's the code:
Executor background = Executors.newSingleThreadExecutor();
Runnable mylookupThread = new Runnable(FilePath, SearchIndex)
{
public void run()
{ MainWindow.this.processFile(this.val$FilePath);
Thread t = new Thread(new lookupThread(MainWindow.arrFile, true, false, this.val$SearchIndex));
t.setName("Lookup");
t.setPriority(10);
t.start();
}
};
background.execute(mylookupThread);
Executor statusThread = Executors.newSingleThreadExecutor();
Runnable myStatusThread = new Runnable()
{
public void run()
{ MainWindow.this.updateStatus();
}
};
statusThread.execute(myStatusThread);
The error pops up on the second line. Help?!?
解决方案
Make mylookupThread
separate class, make it's instance and pass it to Executor
:
class LookupTask implements Runnable {
private final String filePath, searchIndex;
LookupTask(String filePath, String searchIndex) {
this.filePath = filePath;
this.searchIndex = searchIndex;
}
public void run() { ... }
}
...
background.execute(new LookupTask(filePath, searchIndex));
Other way around is to make filePath, searchIndex
final:
final String filePath = ...
final String searchIndex = ...
Executor background = Executors.newSingleThreadExecutor();
Runnable mylookupThread = new Runnable() {
public void run() { MainWindow.this.processFile(filePath);
Thread t = new Thread(new lookupThread(MainWindow.arrFile, true, false, searchIndex));
t.setName("Lookup");
t.setPriority(10);
t.start();
}
};
background.execute(mylookupThread);
这篇关于匿名类如何具有参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文