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

发布于 2024-12-19 22:59:05 字数 766 浏览 1 评论 0原文

我不是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);

第二行弹出错误。帮助?!?

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?!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-12-26 22:59:05

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

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);
从来不烧饼 2024-12-26 22:59:05

new -interface- 形式的匿名类隐式扩展了 Object。您必须使用 Object 的构造函数之一< /a>.只有一个 - 无参数构造函数。

An anonymous class of the form new -interface- implicitly extends Object. You have to use one of the constructors for Object. There is only one - the no-args constructor.

暖树树初阳… 2024-12-26 22:59:05

@Victor 是对的,你可以创建另一个类。您还可以在匿名类中使用 final 变量。像下面这样的东西会起作用。

Executor background = Executors.newSingleThreadExecutor();
private final FilePath filePath = ...;
private final String searchIndex = ...;
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();
    }
};

顺便提一句。在执行器中执行的线程的 Runnable 内部创建线程有点奇怪。不知道为什么你不直接生成 LookupThread 并完全删除匿名类。

@Victor is right that you can create another class. You can also use variables inside an anonymous class that are final. Something like the following will work.

Executor background = Executors.newSingleThreadExecutor();
private final FilePath filePath = ...;
private final String searchIndex = ...;
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();
    }
};

Btw. It's a little strange to create a thread inside the Runnable of a thread executing in an executor. Not sure why you wouldn't just spawn the LookupThread directly and remove the anonymous class altogether.

执手闯天涯 2024-12-26 22:59:05

这就是问题所在(正如您所说:)

Runnable mylookupThread = new Runnable(FilePath, SearchIndex) { ...

发生的情况是我们正在动态定义一个类,并且该类实现了 Runnable 接口。当您使用此语法时,括号中的项目将用作超类的构造函数参数。由于 Runnable 是一个接口,而不是一个类,因此它根本没有构造函数,因此肯定没有任何构造函数需要参数。

也就是说,无论它们应该是什么,它们都不会在匿名类的主体中使用,因此对于第一个近似,您只想完全删除括号内的内容。

This is the problem line (as you said:)

Runnable mylookupThread = new Runnable(FilePath, SearchIndex) { ...

What's happening is that we're defining a class on-the-fly, and that class implements the Runnable interface. When you use this syntax, the items in the parentheses are intended as constructor arguments for the superclass. Since Runnable is an interface, not a class, it has no constructors at all, so there are definitely none that take arguments.

That said, whatever those are supposed to be, they're not used in the body of the anonymous class, so to a first approximation, you want to just drop what's inside the parentheses altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文