java 为什么泛型类型参数说“扩展”比较而不是“实现”?

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

How come generic type parameter says "extends" Comparable not "implements"?

javagenericscomparator

提问by Meow

I tried to write generic function that remove the duplicate elements from array.

我试图编写从数组中删除重复元素的通用函数。

public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {
    //do quicksort
    Arrays.sort(arr);
    ArrayList<E> list = new ArrayList<E>();
    int i;
    for(i=0; i<arr.length-1; i++) {
        if(arr[i].compareTo(arr[i+1]) != 0) { //if not duplicate, add to the list
            list.add(arr[i]);
        }
    }
    list.add(arr[i]); //add last element
    return list;
}

As you can see you can't pass primitive type like int[] array since I am comparing elements by compareTo() method that defined in Comparable interface.

如您所见,您无法传递像 int[] 数组这样的原始类型,因为我正在通过 Comparable 接口中定义的 compareTo() 方法比较元素。

I noticed the first line (method declaration):

我注意到第一行(方法声明):

public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) {

How come it says "extends Comparable" ?

怎么会说“extends Comparable”?

Comparable is an interface so why is it not "implement Comparable"? This is first time I wrote generic function so I'm bit confused about such detail. (any wondering would prevent me from understanding..)

Comparable 是一个接口,那么为什么它不“实现 Comparable”呢?这是我第一次写泛型函数,所以我对这样的细节有点困惑。(任何疑惑都会阻止我理解..)

EDIT: Found this article related to this topic.

编辑:发现这篇文章与此主题相关。

http://www.tutorialspoint.com/java/java_generics.htm

http://www.tutorialspoint.com/java/java_generics.htm

采纳答案by Damian Leszczyński - Vash

If You want to use the thing that implements You just write is as generic parameter

如果你想使用实现你刚刚写的东西作为泛型参数

class Bar extends  Foo<String> { /* Code */}

The wildcard that You are talking about are three

你所说的通配符是三个

  1. "? extends Type": Denotes a family of subtypes of type Type. This is the most useful wildcard
  2. "? super Type": Denotes a family of supertypes of type Type
  3. "?": Denotes the set of all types or any
  1. “? extends Type”:表示 Type 类型的一系列子类型。这是最有用的通配符
  2. “? super Type”:表示Type类型的超类型家族
  3. “?”:表示所有类型或任何类型的集合

You method should look like

你的方法应该看起来像

public static <T extends Comparable<? super T>> Collection<T> sort(T[] list) {

        Collection<T> list = new ArrayList<T>();

         //do quicksort
        Arrays.sort(arr);

        Collection<T> list = new ArrayList<T>();
        int i;
        for(i=0; i<arr.length-1; i++) {
            if(arr[i].compareTo(arr[i+1]) != 0) { //if not duplicate, add to the list
                list.add(arr[i]);
            }
        }
        list.add(arr[i]); //add last element
//btw how do You know that last is not duplicate 
        return list;

}

For detail please visit this page

有关详细信息,请访问此页面

回答by Andrei Fierbinteanu

This is just the convention chosen for generics. When using bounded type parameters you use extends (even though it might mean implements in some cases) or super.

这只是为泛型选择的约定。当使用有界类型参数时,您使用 extends(即使在某些情况下它可能意味着实现)或 super。

You can even do something like <E extends Comparable<E> & Cloneable>to define that the object that would replace the type parameter should implement both those interfaces.

您甚至可以做一些类似的事情<E extends Comparable<E> & Cloneable>来定义将替换类型参数的对象应该实现这两个接口。

回答by Tom Hawtin - tackline

For one thing, Emight be an interface.

一方面,E可能是一个接口。