代码如下:
protected Interface1 varClass1 = new Interface1() {
但我也希望这个匿名嵌套类也扩展类 Base
,类似于:
protected Interface1 varClass1 = new Interface1() extends Base {
....
这在 Java 中可行吗?
最佳答案
匿名类可以只实现一个接口(interface)或扩展一个类。
一种解决方法是创建一个扩展 Base
并实现 Interface1
的命名类,然后将其用作匿名类的基类:
public abstract class Base1 extends Base implements Interface1 {}
...
protected Interface1 varClass1 = new Base1() {
...
关于java - 在Java中,匿名类可以扩展另一个类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479010/