方法返回类型以实现多个接口 [英] Method return type to fulfill multiple interfaces
问题描述
是否可以指定一个返回实现两个或多个接口的对象的方法?
假设我们有以下接口:
interface FooBar {
[Foo]& [Bar] getFooBar();
}
interface Foo {
void doFoo();
}
inteface Bar {
void doBar();
FooBar
需要提供返回一个满足 Foo
类型的实例的方法 getFooBar()
,以及 Bar
。
到目前为止,我尝试过的是使用泛型:
interface FooBar {
}
类SomeImplementor实现FooBar {
private FooAndBarImpl fSomeField;
public< T延伸Foo&酒吧及GT; T getFooBar(){
return fSomeField;
}
}
鉴于 FooAndBarImpl
是由框架或库提供的某种类型,它实现了 Foo
和 Bar
我认为应该工作。但是,它不会,因为FooAndBarImpl不能转换为T。这是为什么?合同隐含的 getFooBar()
不会被破坏,因为我看到它。
另一个解决方案是定义一个新接口扩展 Foo
和 Bar
并将其用作返回类型。我在 getFooBar()
实现中为 fSomeField
返回一个空的包装器时看不出太多意义。 / p>
编辑:
如果有人能解释为什么泛型方法不能没有工作。我只是看不到它。
您可以让T成为类参数:
class SomeImplementor< T extends Foo&酒吧及GT;实现FooBar {
private T fSomeField;
public T getFooBar(){
return fSomeField;
}
}
至于你的泛型方法为什么没有没有工作。让我们创建以下两个类来实现 Foo
和 Bar
:
class A implements Bar,Foo {
private int a;
...
}
class B实现Bar,Foo {
private String b;
...
}
类SomeImplementor实现FooBar {
private A someField;
public< T extends Foo&酒吧及GT; T getFooBar(){
return someField;
$ b因此我们现在应该能够执行以下操作: SomeImplementor s = new SomeImplementor();
A a = s.getFooBar();
B b = s.getFooBar();
尽管 getFooBar()
返回类型A,它不具有对类型B的有效转换( String
成员来自哪里?),即使B满足< T延伸Foo& Bar>
,也就是有效的 T
。
总之,编译器请记住,泛型是一个编译时机制)不能保证类型< T的每个 T
扩展Foo &安培; Bar>
可以为它分配类型 A
。这正是您看到的错误 - 编译器无法将给定的A转换为每个有效的T。
Is it possible to specify a method which returns a object that implements two or multiple interfaces?
Say we have the following interfaces:
interface FooBar {
[Foo] & [Bar] getFooBar();
}
interface Foo {
void doFoo();
}
inteface Bar {
void doBar();
}
Implementors of FooBar
need to provide the method getFooBar()
that returns an instance of a type which fullfills Foo
as well as Bar
.
What I tried so far is to do it with generics:
interface FooBar {
<T extends Foo & Bar> T getFooBar()
}
class SomeImplementor implements FooBar {
private FooAndBarImpl fSomeField;
public <T extends Foo & Bar> T getFooBar() {
return fSomeField;
}
}
Given that FooAndBarImpl
is some type provided by a framework or library and implements Foo
and Bar
this I think should work. However, it doesn't, because "FooAndBarImpl cannot be converted to T". Why is that? The contract implied by getFooBar()
is not broken as I see it.
Another solution would be to define a new interface that extends Foo
and Bar
and to use that as return type. I just don't see much sense in returning a empty wrapper for the fSomeField
in the getFooBar()
implementation.
EDIT:
Would appreciate it if someone could explain why the generics approach doesn't work. I just don't see it.
解决方案 You can make T a class parameter:
class SomeImplementor<T extends Foo & Bar> implements FooBar {
private T fSomeField;
public T getFooBar() {
return fSomeField;
}
}
As to why your generics approach didn't work. Lets create the following two classes that implement Foo
and Bar
:
class A implements Bar, Foo{
private int a;
...
}
class B implements Bar, Foo{
private String b;
...
}
class SomeImplementor implements FooBar {
private A someField;
public <T extends Foo & Bar> T getFooBar() {
return someField;
}
}
So we should now be able to execute the following:
SomeImplementor s = new SomeImplementor();
A a = s.getFooBar();
B b = s.getFooBar();
Although getFooBar()
returns an object of type A, which has no valid cast to type B (where will the String
member come from?), even though B fulfills the requirement of <T extends Foo & Bar>
, i.e. is a valid T
.
In short, the compiler (remember, generics is a compile-time mechanism) can't guarantee that every T
of type <T extends Foo & Bar>
can have an assignment to it of type A
. Which is exactly the error you see - the compiler can't convert the given A to every valid T.
这篇关于方法返回类型以实现多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!