简体   繁体   English

Java Generics:覆盖参数化返回类型不同的方法

[英]Java Generics: override method that differs in parameterized return type

I come from .Net and I'm pretty new to Java development so maybe that's a weird question: 我来自.Net,我对Java开发很新,所以这可能是一个奇怪的问题:

I have a class hierarchy like: 我有一个类层次结构,如:

Superclass implements GenericInterface<Superclass>
    ^
    |
 Subclass

where GenericInterface is pretty straight forward: GenericInterface非常简单:

public interface GenericInterface<T> {
    OtherGenericInterface<T> getOther();
}

and OtherGenericInterface finally uses the type parameter: OtherGenericInterface最后使用了type参数:

public interface OtherGenericInterface<T> {
    List<Object> processType(T left, T right);
}

now when I try to implement the the interface in Superclass I simply return an anonymous type: 现在,当我尝试在Superclass实现接口时,我只返回一个匿名类型:

public class Superclass implements GenericInterface<Superclass> {
    @Override
    public OtherGenericInterface<Superclass> getOther() {
        return new OtherGenericInterface<Superclass>() {
            @Override
            public List<Object> processType(T left, T right) {
                ...
            }
        };
    }
}

That works fine so far but now I try to override the method in the Subclass : 到目前为止工作正常,但现在我尝试覆盖Subclass的方法:

public class Subclass extends Superclass (implements GenericInterface<Subclass>) {
    @Override
    public OtherGenericInterface<Subclass> getOther() {
        ...
    }
}

And in there, I can not override the method with my more specific return type. 在那里,我不能用我更具体的返回类型覆盖该方法。 Even if I re-implement the interface and declare the method in the Superclass as final it is not possible. 即使我重新实现接口并将超类中的方法声明为final也是不可能的。

So my question is: Why isn't OtherInterface<MoreSpecificType> a more specific, or at least the same type (due to type erasure) because that would be the requirement to override the method right?. 所以我的问题是:为什么OtherInterface<MoreSpecificType>不是更具体,或者至少是相同类型(由于类型擦除),因为这是要求覆盖方法的权利?

This demonstrates a common misconception with Java generics - believing that a match of a class will also match subclasses (like types of parameters). 这表明了对Java泛型的一种常见误解 - 认为类的匹配也将匹配子类(如参数类型)。 That is not the case. 事实并非如此。 Java generics are designed to ensure that the types match exactly . Java泛型旨在确保类型完全匹配。 If you want wriggle room you must specify and define what room you want and how much. 如果你想要蠕动空间,你必须指定和定义你想要的房间和数量。

Here's a version that allows you to do what you want by exactly specifying the signature as <T extends Superclass> . 这是一个版本,允许您通过将签名精确指定为<T extends Superclass>来执行您想要的操作。 This may not be exactly what you are looking for but I hope it points you in the right direction. 这可能不是您正在寻找的,但我希望它指出您正确的方向。

public interface OtherGenericInterface<T> {

    List<Object> processType(T left, T right);
}

public interface GenericInterface<T> {

    OtherGenericInterface<T> getOther();
}

public class Superclass<T extends Superclass> implements GenericInterface<T> {

    @Override
    public OtherGenericInterface<T> getOther() {
        return new OtherGenericInterface<T>() {
            @Override
            public List<Object> processType(Superclass left, Superclass right) {
                return null;
            }
        };
    }
}

public class Subclass extends Superclass {

    @Override
    public OtherGenericInterface<Subclass> getOther() {
        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM