How can an anonymous class have arguments?

后端 未结 4 1623
庸人自扰
庸人自扰 2021-01-22 05:42

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

4条回答
  •  余生分开走
    2021-01-22 06:21

    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.

提交回复
热议问题