一个匿名课程的多重继承

一个匿名类如何实现两个(或多个)接口?或者,它如何能够 都有扩展类 还有实现一个接口? 例如,我想创建一个扩展了两个接口的匿名类对象:

    // Java 10 "var" is used since I don't know how to specify its type
var lazilyInitializedFileNameSupplier = (new Supplier<String> implements AutoCloseable)() {
private String generatedFileName;
@Override
public String get() { // Generate file only once
if (generatedFileName == null) {
generatedFileName = generateFile();
}
return generatedFileName;
}
@Override
public void close() throws Exception { // Clean up
if (generatedFileName != null) {
// Delete the file if it was generated
generatedFileName = null;
}
}
};

然后我可以在 try-with-resources 块中将它作为延迟初始化的实用工具类使用:

        try (lazilyInitializedFileNameSupplier) {
// Some complex logic that might or might not
// invoke the code that creates the file
if (checkIfNeedToProcessFile()) {
doSomething(lazilyInitializedFileNameSupplier.get());
}
if (checkIfStillNeedFile()) {
doSomethingElse(lazilyInitializedFileNameSupplier.get());
}
}
// By now we are sure that even if the file was generated, it doesn't exist anymore

我不想创建一个内部类,因为我非常确定这个类不会在任何地方使用,除了我需要在其中使用它的方法(我还想使用在那个方法中声明的本地变量,它可能是 var类型)。

110585 次浏览

Anonymous classes must extend or implement something, like any other Java class, even if it's just java.lang.Object.

For example:

Runnable r = new Runnable() {
public void run() { ... }
};

Here, r is an object of an anonymous class which implements Runnable.

An anonymous class can extend another class using the same syntax:

SomeClass x = new SomeClass() {
...
};

What you can't do is implement more than one interface. You need a named class to do that. Neither an anonymous inner class, nor a named class, however, can extend more than one class.

An anonymous class usually implements an interface:

new Runnable() { // implements Runnable!
public void run() {}
}


JFrame.addWindowListener( new WindowAdapter() { // extends  class
} );

If you mean whether you can implement 2 or more interfaces, than I think that's not possible. You can then make a private interface which combines the two. Though I cannot easily imagine why you would want an anonymous class to have that:

 public class MyClass {
private interface MyInterface extends Runnable, WindowListener {
}


Runnable r = new MyInterface() {
// your anonymous class which implements 2 interaces
}


}

Anonymous classes always extend superclass or implements interfaces. for example:

button.addActionListener(new ActionListener(){ // ActionListener is an interface
public void actionPerformed(ActionEvent e){
}
});

Moreover, although anonymous class cannot implement multiple interfaces, you can create an interface that extends other interface and let your anonymous class to implement it.

// The interface
interface Blah {
void something();
}


...


// Something that expects an object implementing that interface
void chewOnIt(Blah b) {
b.something();
}


...


// Let's provide an object of an anonymous class
chewOnIt(
new Blah() {
@Override
void something() { System.out.println("Anonymous something!"); }
}
);

An anonymous class is extending or implementing while creating its object For example :

Interface in = new InterFace()
{


..............


}

Here anonymous class is implementing Interface.

Class cl = new Class(){


.................


}

here anonymous Class is extending a abstract Class.

I guess nobody understood the question. I guess what this guy wanted was something like this:

return new (class implements MyInterface {
@Override
public void myInterfaceMethod() { /*do something*/ }
});

because this would allow things like multiple interface implementations:

return new (class implements MyInterface, AnotherInterface {
@Override
public void myInterfaceMethod() { /*do something*/ }


@Override
public void anotherInterfaceMethod() { /*do something*/ }
});

this would be really nice indeed; but that's not allowed in Java.

What you can do is use local classes inside method blocks:

public AnotherInterface createAnotherInterface() {
class LocalClass implements MyInterface, AnotherInterface {
@Override
public void myInterfaceMethod() { /*do something*/ }


@Override
public void anotherInterfaceMethod() { /*do something*/ }
}
return new LocalClass();
}