简体   繁体   English

覆盖通用方法

[英]Override generic method

According to the JLS (Java Language Specification): 根据JLS (Java语言规范):

The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. 子签名的概念旨在表示两种方法之间的关系,这些方法的签名不完全相同,但是其中一个可以覆盖另一个方法。 Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. 具体来说,它允许签名不使用泛型类型的方法覆盖该方法的任何泛化版本。

This code is based on the JLS example: 该代码基于JLS示例:

interface CollectionConverter<U> {
    <T> List<T> toList(Collection<T> c);

    void fooMethod(Class<?> c);

    <E>Comparable<E> method3(E e);

    Comparable<U> method4(U u);
}

class Overrider implements CollectionConverter<Integer> {
    @Override
    public List toList(Collection c) {
        return null;
    }

    @Override
    public void fooMethod(Class c) {

    }

    @Override
    public  Comparable method3(Object o) {
        return null;
    }

    @Override
    // compile error, have to change Object to Integer 
    public Comparable method4(Object u) {                       

        return null;
    }
}

According to the JLS, I understand why the first three methods work well, but I can't figure out why method4 has this compilation error: 根据JLS,我了解为什么前三种方法可以很好地工作,但是我无法弄清楚method4为何存在此编译错误:

The method method4(Object) of type Overrider must override or implement a supertype method. 类型为Overrider的method4(Object)方法必须重写或实现一个超类型方法。

The signature of method4 in CollectionConverter is CollectionConvertermethod4的签名为

Comparable<U> method4(U u);

You declare Overrider to implement CollectionConverter<Integer> , thereby binding the type parameter U to Integer . 您声明Overrider来实现CollectionConverter<Integer> ,从而将类型参数U绑定到Integer The signature then becomes: 签名将变为:

Comparable<Integer> method4(Integer u);

You can declare a method4(Object u) in Overrider , but that method signature does not override method4(Integer u) specified in the interface any more than it would if you weren't using generics at all. 您可以在Overrider声明method4(Object u) ,但与根本不使用泛型的情况相比,该方法签名不会覆盖接口中指定的method4(Integer u)

The problem is the type variable U is bound to Integer at that point. 问题在于此时类型变量U已绑定到Integer If you change the declaration to 如果将声明更改为

public Comparable method4(Integer u) ...

it is an override 这是一个替代

Because in the interface, method4 is declared with the same type parameter as the interface (U). 因为在接口中,method4的声明类型与接口(U)相同。 If you change it to something else, it should work. 如果将其更改为其他内容,则应该可以使用。

For example 例如

<A> Comparable<A> method4(A a);

For the specific case of wanting to override a method with a generic param, which does not have a generic return type, you need to make sure type of the param is actually a child of the that generic type. 对于想要用通用参数覆盖没有通用返回类型的方法的特定情况,您需要确保该参数的类型实际上是该通用类型的子级。

For example: 例如:

protected Response updateResource(long id, T payload, RequestContext context){}

is overriden by 被覆盖

@Override
protected Response updateResource(long id, Payload payload, RequestContext context){}

Thumbs up for the IntelliJ IDEA's Code > Generate... > Override Methods... 赞成IntelliJ IDEA的代码>生成...>覆盖方法...

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

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