I have two interfaces:
我有两个接口:
interface A {
void foo();
}
interface B {
void bar();
}
I am able to create anonymous instances of classes implementing either of these interfaces like so:
我能够创建实现这些接口的类的匿名实例,如下所示:
new A() {
void foo() {}
}
or:
要么:
new B() {
void bar() {}
}
I want to create an anonymous class that implements both interfaces. Something like (the fictitious):
我想创建一个实现两个接口的匿名类。像(虚构的):
new A implements B {
void foo() {}
void bar() {}
}
This obviously gives a compile error: "B cannot be resolved to a type".
这显然会产生编译错误:“B无法解析为类型”。
The workaround is quite simple:
解决方法很简单:
class Aggregate implements A, B {
void foo() {}
void bar() {}
}
I then use Aggregate
where ever I would have used the anonymous class.
然后我使用Aggregate,我将使用匿名类。
I was wondering if it is even legal for an anonymous class to implement two interfaces.
我想知道一个匿名类实现两个接口是否合法。
4 个解决方案
#1
54
"An anonymous inner class can extend one subclass or implement one interface. Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot do both. In other words, it cannot both extend a class and implement an interface, nor can it implement more than one interface. " (http://scjp.wikidot.com/nested-classes)
“匿名内部类可以扩展一个子类或实现一个接口。与非匿名类(内部或其他)不同,匿名内部类不能同时执行这两种操作。换句话说,它不能同时扩展类并实现接口,也不能它实现了多个接口。“(http://scjp.wikidot.com/nested-classes)
#2
15
If you are determined to do this, you could declare a third interface, C:
如果您决定这样做,您可以声明第三个接口,C:
public interface C extends A, B {
}
In this way, you can declare a single anonymous inner class, which is an implementation of C.
通过这种方式,您可以声明一个匿名内部类,这是C的一个实现。
A complete example might look like:
完整的示例可能如下所示:
public class MyClass {
public interface A {
void foo();
}
public interface B {
void bar();
}
public interface C extends A, B {
void baz();
}
public void doIt(C c) {
c.foo();
c.bar();
c.baz();
}
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.doIt(new C() {
@Override
public void foo() {
System.out.println("foo()");
}
@Override
public void bar() {
System.out.println("bar()");
}
@Override
public void baz() {
System.out.println("baz()");
}
});
}
}
The output of this example is:
这个例子的输出是:
foo()
bar()
baz()
#3
7
For save some keystrokes (for example if the interfaces have a lot of methods) you can use
为了节省一些键击(例如,如果接口有很多方法),您可以使用
abstract class Aggregate implements A,B{
}
new MyObject extends Aggregate{
void foo(){}
void bar(){}
}
Notice the key is to declare the Aggregate as abstract
请注意,关键是将Aggregate声明为抽象
#4
1
Note that you can make a named local class that implement the two interfaces:
请注意,您可以创建一个实现两个接口的命名本地类:
void method() {
class Aggregate implements A, B {
void foo() {}
void bar() {}
}
A a = new Aggregate();
B b = new Aggregate();
}
This save you from doing a class-level or top-level class declaration.
这使您无法进行类级别或*类声明。
The result is called a local class. Local classes declared in instance methods are also inner classes, which means that they can reference the containing object instance.
结果称为本地类。在实例方法中声明的本地类也是内部类,这意味着它们可以引用包含对象实例。
#1
54
"An anonymous inner class can extend one subclass or implement one interface. Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot do both. In other words, it cannot both extend a class and implement an interface, nor can it implement more than one interface. " (http://scjp.wikidot.com/nested-classes)
“匿名内部类可以扩展一个子类或实现一个接口。与非匿名类(内部或其他)不同,匿名内部类不能同时执行这两种操作。换句话说,它不能同时扩展类并实现接口,也不能它实现了多个接口。“(http://scjp.wikidot.com/nested-classes)
#2
15
If you are determined to do this, you could declare a third interface, C:
如果您决定这样做,您可以声明第三个接口,C:
public interface C extends A, B {
}
In this way, you can declare a single anonymous inner class, which is an implementation of C.
通过这种方式,您可以声明一个匿名内部类,这是C的一个实现。
A complete example might look like:
完整的示例可能如下所示:
public class MyClass {
public interface A {
void foo();
}
public interface B {
void bar();
}
public interface C extends A, B {
void baz();
}
public void doIt(C c) {
c.foo();
c.bar();
c.baz();
}
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.doIt(new C() {
@Override
public void foo() {
System.out.println("foo()");
}
@Override
public void bar() {
System.out.println("bar()");
}
@Override
public void baz() {
System.out.println("baz()");
}
});
}
}
The output of this example is:
这个例子的输出是:
foo()
bar()
baz()
#3
7
For save some keystrokes (for example if the interfaces have a lot of methods) you can use
为了节省一些键击(例如,如果接口有很多方法),您可以使用
abstract class Aggregate implements A,B{
}
new MyObject extends Aggregate{
void foo(){}
void bar(){}
}
Notice the key is to declare the Aggregate as abstract
请注意,关键是将Aggregate声明为抽象
#4
1
Note that you can make a named local class that implement the two interfaces:
请注意,您可以创建一个实现两个接口的命名本地类:
void method() {
class Aggregate implements A, B {
void foo() {}
void bar() {}
}
A a = new Aggregate();
B b = new Aggregate();
}
This save you from doing a class-level or top-level class declaration.
这使您无法进行类级别或*类声明。
The result is called a local class. Local classes declared in instance methods are also inner classes, which means that they can reference the containing object instance.
结果称为本地类。在实例方法中声明的本地类也是内部类,这意味着它们可以引用包含对象实例。