匿名类怎么可以有参数呢?
我不是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
mylookupThread
设为单独的类,使其成为实例并将其传递给Executor
:另一种方法是将
filePath, searchIndex
设为最终的:Make
mylookupThread
separate class, make it's instance and pass it toExecutor
:Other way around is to make
filePath, searchIndex
final: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.
@Victor 是对的,你可以创建另一个类。您还可以在匿名类中使用
final
变量。像下面这样的东西会起作用。顺便提一句。在执行器中执行的线程的 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.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 theLookupThread
directly and remove the anonymous class altogether.这就是问题所在(正如您所说:)
发生的情况是我们正在动态定义一个类,并且该类实现了 Runnable 接口。当您使用此语法时,括号中的项目将用作超类的构造函数参数。由于 Runnable 是一个接口,而不是一个类,因此它根本没有构造函数,因此肯定没有任何构造函数需要参数。
也就是说,无论它们应该是什么,它们都不会在匿名类的主体中使用,因此对于第一个近似,您只想完全删除括号内的内容。
This is the problem line (as you said:)
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. SinceRunnable
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.