java Java泛型 - 覆盖抽象方法并具有子类的返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6450762/
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 generics - overriding an abstract method and having return type of the subclass
提问by Numeron
I am trying to create a set up where a set of subclasses override a superclass. This superclass contains an abstract method - the return type of which would ideally be that of the object from which this method was called, such that it effectively behaves like this:
我正在尝试创建一个设置,其中一组子类覆盖超类。这个超类包含一个抽象方法 - 理想情况下,该方法的返回类型应该是调用该方法的对象的返回类型,这样它的有效行为如下:
public abstract class SuperClass{
public abstract SuperClass getSelf();
}
public class SubClass extends SuperClass{
@Override
public SubClass getSelf(){
return this;
}
}
I am unsure if such a thing is possible, as I think return types always have to be the same in order for the override to work - however I have been thinking the answer, should one exist, lies somewhere along this line...
我不确定这样的事情是否可能,因为我认为返回类型必须始终相同才能使覆盖起作用 - 但是我一直在想答案,如果存在的话,就在这条线的某个地方......
public abstract class SuperClass{
public abstract <? extends SuperClass> getSelf();
}
public class SubClass extends SuperClass{
@Override
public SubClass getSelf(){
return this;
}
}
Thanks for any help.
谢谢你的帮助。
edit: added extends SuperClass to SubClass, duh
编辑:添加将超类扩展到子类,呃
采纳答案by Rob Harrop
This will work:
这将起作用:
public abstract class SuperClass{
public abstract SuperClass getSelf();
}
public class SubClass extends SuperClass{
@Override
public SubClass getSelf(){
return this;
}
}
Notice I've added extends SuperClass
to your SubClass
definition. The return type of getSelf
is referred to as a covariant return type.
请注意,我已添加extends SuperClass
到您的SubClass
定义中。的返回类型getSelf
称为协变返回类型。
回答by plouh
How about this:
这个怎么样:
public abstract class SuperClass<T extends SuperClass<?>> {
public abstract T getSelf();
}
public class SubClass extends SuperClass<SubClass> {
public SubClass getSelf() {
return this;
}
}
I know it's quite repetitive and nothing bounds the type to be the same SubClass
instance, because also AnotherSubClass
would satisfy the bound, but at least it should do the trick.
我知道它是非常重复的,并且没有任何东西将类型限制为同一个SubClass
实例,因为也AnotherSubClass
可以满足这个限制,但至少它应该可以解决问题。
回答by Nick
Here is how to do it (Since JDK 1.5 there is this thing called covariant return types, where something like this is possible).
这是如何做到的(从 JDK 1.5 开始,有一种叫做协变返回类型的东西,这样的事情是可能的)。
abstract class SuperClass<T extends SuperClass<T>>{
public abstract T getSelf();
}
class SubClass extends SuperClass<SubClass> {
public SubClass getSelf() { return this; }
}
public class Generics {
public static void main(String[] args) {
System.out.println(new SubClass().getSelf());
}
}
Notice a similar class generic definition with Enum (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Enum.html)
注意与 Enum 类似的类通用定义 (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Enum.html)
See what happens behind the scenes (by using javap SuperClass SubClass):
看看幕后发生了什么(通过使用javap SuperClass SubClass):
class SubClass extends SuperClass{
SubClass();
public SubClass getSelf();
public SuperClass getSelf();
}
abstract class SuperClass extends java.lang.Object{
SuperClass();
public abstract SuperClass getSelf();
}
Notice how the subclass method has a different return type, which is a subtype of the super method return type.
注意子类方法如何具有不同的返回类型,它是超级方法返回类型的子类型。
Btw, notice that public SuperClass getSelf();
in class SubClass
is actually a syntheticmethod.
顺便说一句,请注意public SuperClass getSelf();
in classSubClass
实际上是一种合成方法。