Java 接口和返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2413829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java interfaces and return types
提问by Jonathan
Consider I have the following interface:
考虑我有以下接口:
public interface A { public void b(); }
However I want each of the classes that implement it to have a different return type for the method b().
但是,我希望实现它的每个类都对方法 b() 具有不同的返回类型。
Examples:
例子:
public class C {
public C b() {}
}
public class D {
public D b() {}
}
How would I define my interface so that this was possible?
我将如何定义我的接口以便这成为可能?
采纳答案by Matt McHenry
If the return type must be the type of the class that implements the interface, then what you want is called an F-bounded type:
如果返回类型必须是实现接口的类的类型,那么你想要的叫做F-bounded type:
public interface A<T extends A<T>>{ public T b(); }
public class C implements A<C>{
public C b() { ... }
}
public class D implements A<D>{
public D b() { ... }
}
In words, A
is declaring a type parameter T
that will take on the value of each concrete type that implements A
. This is typically used to declare things like clone()
or copy()
methods that are well-typed. As another example, it's used by java.lang.Enum
to declare that each enum's inherited compareTo(E)
method applies only to other enums of that particular type.
换句话说,A
就是声明一个类型参数T
,该参数将采用实现 的每个具体类型的值A
。这通常用于声明类型良好的东西clone()
或copy()
方法。作为另一个示例,它用于java.lang.Enum
声明每个枚举的继承compareTo(E)
方法仅适用于该特定类型的其他枚举。
If you use this pattern often enough, you'll run into scenarios where you need this
to be of type T
. At first glance it might seem obvious that it is1, but you'll actually need to declare an abstract T getThis()
method which implementers will have to trivially implement as return this
.
如果你经常使用这种模式,你会遇到需要this
类型的场景T
。乍一看似乎很明显它是1,但您实际上需要声明一个abstract T getThis()
方法,实现者必须简单地将其实现为return this
。
[1] As commenters have pointed out, it is possible to do something sneaky like X implements A<Y>
if X
and Y
cooperate properly. The presence of a T getThis()
method makes it even clearer that X
is circumventing the intentions of the author of the A
interface.
[1] 正如评论者指出的那样,可以做一些偷偷摸摸的事情,比如X implements A<Y>
ifX
并Y
适当地合作。T getThis()
方法的存在使得X
绕过A
接口作者意图的方法变得更加清晰。
回答by Cam
Generics.
泛型。
public interface A<E>{
public E b();
}
public class C implements A<C>{
public C b(){
return new C();
}
}
public class D implements A<D>{
public D b(){
return new D();
}
}
Search up generics for more details, but (very) basically, what's happening is that A
leaves E
's type up to the implementing clases (C
and D
).
搜索泛型以获取更多详细信息,但是(非常)基本上,正在发生的事情是将的类型A
留给E
实现类(C
和D
)。
So basically A doesn't know (and doesn't have to know) what E might be in any given implementation.
所以基本上 A 不知道(也不必知道)E 在任何给定的实现中可能是什么。
回答by meriton
Since Java supports covariant return types(since Java 1.5), you can do:
由于 Java支持协变返回类型(自 Java 1.5 起),您可以执行以下操作:
public interface A { public Object b(); }