Java 使用泛型类实现 Comparable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21544716/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:17:20  来源:igfitidea点击:

Implementing Comparable with a generic class

javagenericsinterfacecomparableraw-types

提问by frank.liu

I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T. In order to implement the interface, I delegate the comparison to T. Here is my code:

我想定义一个实现通用 Comparable 接口的类。在我的课堂上,我还定义了一个泛型类型元素T。为了实现接口,我将比较委托给T. 这是我的代码:

public class Item<T extends Comparable<T>> implements Comparable<Item> {

    private int s;
    private T t;

    public T getT() {
        return t;
    }

    @Override
    public int compareTo(Item o) {
        return getT().compareTo(o.getT());
    }
}

When I try to compile it, I get the following error information:

当我尝试编译它时,我收到以下错误信息:

Item.java:11: error: method compareTo in interface Comparable<T#2> cannot be applied to given types;
        return getT().compareTo(o.getT());
                     ^
  required: T#1
  found: Comparable
  reason: actual argument Comparable cannot be converted to T#1 by method invocation conversion
  where T#1,T#2 are type-variables:
    T#1 extends Comparable<T#1> declared in class Item
    T#2 extends Object declared in interface Comparable
1 error

Can anybody tell me why and how to fix it?

谁能告诉我为什么以及如何解决它?

采纳答案by Radiodef

Item(without any type argument) is a raw type, so:

Item(没有任何类型参数)是一个原始类型,所以:

  1. We could pass any kind of Itemto Item.compareTo. For example, this would compile:

    new Item<String>().compareTo(new Item<Integer>())
    
  2. The method o.getT()returns Comparableinstead of T, which causes the compilation error.

    In the example under the 1st point, after passing Item<Integer>to Item.compareTo, we would then erroneously pass an Integerto String.compareTo. The compilation error prevents us from writing the code which does that.

  1. 我们可以传递任何类型的Itemto Item.compareTo。例如,这将编译:

    new Item<String>().compareTo(new Item<Integer>())
    
  2. 该方法o.getT()返回Comparable而不是T,这会导致编译错误。

    在第 1 点下的示例中,在传递Item<Integer>to 之后Item.compareTo,我们会错误地传递Integerto String.compareTo。编译错误阻止我们编写执行此操作的代码。

I think you just need to remove the raw types:

我认为您只需要删除原始类型:

public class Item<T extends Comparable<T>>
implements Comparable<Item<T>> {

    ...

    @Override
    public int compareTo(Item<T> o) {
        return getT().compareTo(o.getT());
    }
}

回答by Njol

You're using raw types in your class definition (Item<T>is generic, but you're omitting the type parameter <T>), change it to:

您在类定义中使用原始类型(Item<T>是通用的,但省略了类型参数<T>),将其更改为:

class Item<T extends Comparable<T>> implements Comparable<Item<T>>

(Note the last <T>)

(注意最后一个<T>

The compareTomethod will then have to be changed as well:

compareTo然后,该方法也必须更改:

public int compareTo(Item<T> o) { // again, use generics
    return getT().compareTo(o.getT());
}

回答by aditya

I think, this makes more sense. I have compiled and tested the following :

我认为,这更有意义。我已经编译并测试了以下内容:

class Item<E extends Comparable<E>> implements Comparable<E> {

 private int s;
 private E t;

 public E getE() {
     return t;
 }

 @Override
 public int compareTo(E e) {
     return getE().compareTo(e);
 }

 public int compareTo(Item<E> other)
    {
        return getE().compareTo(other.getE());
    }

 }

Notice that you now need to have two compareTo methods.

请注意,您现在需要有两个 compareTo 方法。