什么是<?超级T>句法? [英] What is <? super T> syntax?

查看:149
本文介绍了什么是<?超级T>句法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解以下语法:

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

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

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

T extends Comparable< ? super T>

表示。

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

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

推荐答案

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

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).

在这种特殊情况下,它所说的是类型必须实现其自身或其超类的可比性。所以考虑java.util.Date。它实现了 Comparable< Date> 。但是java.sql.Date怎么样?它还实现 Comparable< java.util.Date>

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.

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

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.

这篇关于什么是&lt;?超级T&gt;句法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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