Java 什么是<? 超级T>语法?

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

What is <? super T> syntax?

java

提问by ShrimpCrackers

I'm having trouble understanding the following syntax:

我无法理解以下语法:

public class SortedList< T extends Comparable< ? super T> > extends LinkedList< T >

I see that class SortedList extends LinkedList. I just don't know what

我看到 SortedList 类扩展了 LinkedList。我只是不知道什么

T extends Comparable< ? super T>

means.

方法。

My understanding of it so far is that type T must be a type that implements Comparable...but what is < ? super T >?

到目前为止,我对它的理解是类型 T 必须是实现 Comparable 的类型……但是什么是< ? super T >

采纳答案by Yishai

super in Generics is the opposite of extends. Instead of saying the comparable's generic type has to be a subclass of T, it is saying it has to be a superclass of T. The distinction is important because extends tells you what you can get out of a class (you get at least this, perhaps a subclass). super tells you what you can put into the class (at most this, perhaps a superclass).

泛型中的 super 与扩展相反。与其说可比较的泛型类型必须是 T 的子类,而是说它必须是 T 的超类。 区别很重要,因为 extends 告诉你你可以从一个类中得到什么(你至少得到这个,也许是一个子类)。super 告诉你你可以在类中放入什么(最多这个,可能是一个超类)。

In this specific case, what it is saying is that the type has to implement comparable of itself or its superclass. So consider java.util.Date. It implements Comparable<Date>. But what about java.sql.Date? It implements Comparable<java.util.Date>as well.

在这种特定情况下,它所说的是该类型必须实现其自身或其超类的可比性。所以考虑java.util.Date。它实现Comparable<Date>. 但是java.sql.Date呢?它也执行Comparable<java.util.Date>

Without the super signature, SortedList would not be able accept the type of java.sql.Date, because it doesn't implement a Comparable of itself, but rather of a super class of itself.

如果没有超级签名,SortedList 将无法接受 java.sql.Date 的类型,因为它不实现自身的 Comparable,而是实现自身的超类。

回答by polygenelubricants

It's a lower-bounded wildcard.

这是一个下界通配符。

JLS 4.5.1 Type Arguments and Wildcards

JLS 4.5.1 类型参数和通配符

Wildcards are useful in situations where only partial knowledge about the type parameter is required. [...] An upper bound is signified by the syntax:

? extends B

where Bis the upper bound. [...] it is permissible to declare lower bounds on a wildcard, using the syntax:

? super B

where Bis a lower bound.

在只需要有关类型参数的部分知识的情况下,通配符很有用。[...] 上限由以下语法表示:

? extends B

B上界在哪里。[...] 允许使用以下语法在通配符上声明下限:

? super B

哪里B是下界。

A List<? super Integer>, for example, includes List<Integer>, List<Number>, and List<Object>.

List<? super Integer>,例如,包括List<Integer>List<Number>,和List<Object>

Wildcards are used to make generics more powerful and flexible; bounds are used to maintain type safety.

通配符用于使泛型更加强大和灵活;边界用于维护类型安全。

See also

也可以看看



As to how this is useful in <T extends Comparable<? super T>>, it's when you have something like Cat extends Animal implements Comparable<Animal>.

至于如何这是有用的<T extends Comparable<? super T>>,当你有这样的事情的Cat extends Animal implements Comparable<Animal>

Look at the signature of Collections.sort

看签名 Collections.sort

public static <T extends Comparable<? super T>> void sort(List<T> list)

Therefore, with a List<Cat> listOfCat, you can now Collections.sort(listOfCat).

因此,使用List<Cat> listOfCat,您现在可以Collections.sort(listOfCat).

Had it been declared as follows:

如果它被声明如下:

public static <T extends Comparable<T>> void sort(List<T> list)

then you'd have to have Cat implements Comparable<Cat>to use sort. By using the ? super Tbounded wildcard, Collections.sortbecomes more flexible.

那么你就必须Cat implements Comparable<Cat>使用sort. 通过使用? super T有界通配符,Collections.sort变得更加灵活。

See also

也可以看看

  • Effective Java 2nd Edition, Item 28: Use bounded wildcards to increase API flexibility
    • Also, PECS principle: "producer extendsconsumer super"
  • Effective Java 2nd Edition,Item 28:使用有界通配符来增加 API 的灵活性
    • 还有,PECS 原则:“生产者extends消费者super

回答by Petar Minchev

It means that Tmust implement Comparable<T itself or one of T's superclasses>The sense is that because SortedListis sorted, it must know how to compare two classes of its generics Tparameter. That's why Tmust implement Comparable<T itself or one of T's superclasses>

这意味着T必须实现Comparable<T itself or one of T's superclasses>意义是因为SortedList是排序的,所以它必须知道如何比较其泛型T参数的两个类。这就是为什么T必须实施Comparable<T itself or one of T's superclasses>

回答by Epitaph

Consider the following example:

考虑以下示例:

  1. Using a type parameter defined in the class declaration

    public class ArrayList extends AbstractList ... {
    public boolean add(E o) // You can use the "E" here ONLY because it's already been defined as part of the class

  2. Using a type parameter that was NOT defined in the class declaration

    public <T extends Animal> void takeThing(ArrayList<T> list)
                                                // Here we can use <T> because we declared "T" earlier in the method declaration                  
    

    If the class itself doesn't use a type parameter, you can still specify one for a method, by declaring it in a really unusual (but available) space - before the return type. This method says that T can be "any type of Animal".

  1. 使用类声明中定义的类型参数

    public class ArrayList extends AbstractList ... {
    public boolean add(E o) // 你只能在这里使用“E”,因为它已经被定义为类的一部分

  2. 使用未在类声明中定义的类型参数

    public <T extends Animal> void takeThing(ArrayList<T> list)
                                                // Here we can use <T> because we declared "T" earlier in the method declaration                  
    

    如果类本身不使用类型参数,您仍然可以为方法指定一个,方法是在一个非常不寻常(但可用)的空间中声明它 - 在返回类型之前。这个方法说 T 可以是“任何类型的动物”。

NOTE:

笔记:

public <T extends Animal> void takeThing(ArrayList<T> list)             

is NOT same as              

public void takeThing(ArrayList<Animal> list)

Both are legal, but they are different. The first one indicates that you can pass in a ArrayList object instantiated as Animal or any Animal subtype like ArrayList, ArrayList or ArrayList. But, you can only pass ArrayList in the second, and NOT any of the subtypes.

两者都是合法的,但它们是不同的。第一个表示您可以传入实例化为 Animal 或任何 Animal 子类型(如 ArrayList、ArrayList 或 ArrayList)的 ArrayList 对象。但是,您只能在第二个中传递 ArrayList,而不能传递任何子类型。

回答by John Smith

It means that the type Tmust implement Comparableof Tor one of its super classes.

这意味着该类型T必须实现ComparableT还是它的超类之一。

For example, if Aextends B, if you want to use SortedList<A>, Amust implement Comparable<A>or Comparable<B>, or in fact just Comparable.

例如,如果Aextends B,如果你想使用SortedList<A>A必须实现Comparable<A>or Comparable<B>,或者实际上只是Comparable

This allows the list of As to be constructed with any valid comparator.

这允许A使用任何有效的比较器构造 s的列表。