如何在签名中正确覆盖泛型的抽象方法 [英] How to properly override an abstract method with generics in the signature

查看:170
本文介绍了如何在签名中正确覆盖泛型的抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我明白如何做到这一点,但我得到了一些意想不到的行为,所以显然我错过了一些东西。

基类:

  public abstract class Base< T> 
{
abstract public void foo(List< ;? extends T> l);



$ b

派生类:

  public class Derived< T>扩展Base 
{
@Override
public void foo(List< ;? extends T> l){return; }基类遵守的很好,但是当我编译Derived类时,我得到了:


Derived.java:3:Derived不是抽象的,并且不会覆盖基本的抽象方法foo(java.util.List) / p>

public class Derived extends Base

^

Derived.java:5:方法不会覆盖或实现一个方法超类型

  @Override 
^



<2>错误

参数 List的泛型<?延长T> 似乎是问题的原因。如果我用基本类型 int 替换这两个签名中的部分,它就会正常工作。



有人可以告诉我什么是在这里?

解决方案

您应该这样做

pre> public class Derived< T>延伸Base< T>

您需要指定< T> 对于 Base ,否则您将不得不通过简单地声明 List 显示泛型


I thought I understood how to do this but I'm getting some unexpected behavior so apparently I'm missing something. Here's the problem boiled down.

Base Class:

public abstract class Base<T>
{
    abstract public void foo(List<? extends T> l);
}

Derived Class:

public class Derived<T> extends Base
{
    @Override
    public void foo(List<? extends T> l) { return; }
}

The Base class complies fine, but when I compile the Derived class I get:

Derived.java:3: Derived is not abstract and does not override abstract method foo(java.util.List) in Base

public class Derived extends Base
^
Derived.java:5: method does not override or implement a method from a supertype

@Override  
^  

2 errors

The generics of the parameter List<? extends T> appears to be the cause of the problem. If I replace that part in both signatures with the basic type int it comples fine.

Can anybody tell me what's going on here?

解决方案

You should do

public class Derived<T> extends Base<T>

You need to specify <T> for Base otherwise you will have to override method by simply declaring List i.e.without generics

这篇关于如何在签名中正确覆盖泛型的抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆