How can an anonymous class have arguments?

时光怂恿深爱的人放手 提交于 2019-12-31 03:17:05

问题


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


回答1:


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



回答2:


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.




回答3:


@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.




回答4:


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.



来源:https://stackoverflow.com/questions/8402876/how-can-an-anonymous-class-have-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!