是否可以指定一个返回实现两个或多个接口的对象的方法?
说我们有以下接口:
interface FooBar {
[Foo] & [Bar] getFooBar();
}
interface Foo {
void doFoo();
}
inteface Bar {
void doBar();
}
FooBar的实现者需要提供getFooBar()方法,该方法返回一个完整填充Foo类型的实例.
到目前为止我所尝试的是用泛型:
interface FooBar {
T getFooBar()
}
class SomeImplementor implements FooBar {
private FooAndBarImpl fSomeField;
public T getFooBar() {
return fSomeField;
}
}
鉴于FooAndBarImpl是由框架或库提供的一些类型,并实现了Foo和Bar这个我认为应该工作.但是,它并不是因为“FooAndBarImpl不能转换为T”.这是为什么? getFooBar()暗示的合同不会像我看到的那样被破坏.
另一个解决方案是定义一个扩展Foo和Bar的新接口,并将其用作返回类型.在getFooBar()实现中为fSomeField返回一个空的包装器时,我看起来没有什么意义.
感谢任何其他建议和/或解释.
编辑:
如果有人可以解释为什么仿制药方法不起作用,会感激不尽.我只是看不到它.