匿名类怎么可以有参数呢?

2023-11-30

我不是java人,但我继承了一些需要修补的代码。我将源代码拉入 netbeans 中,但收到错误:匿名类实现接口;不能有争论。

这是代码:

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);

错误出现在第二行。帮助?!?


Make 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 = ...
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

匿名类怎么可以有参数呢? 的相关文章

随机推荐