简体   繁体   English

为什么T扩展了Comparable <? super T> 包括T? 含义包括可比较 <T> ?

[英]Why does T extends Comparable <? super T> include T? Meaning includes Comparable<T>?

class BinarySearch<T extends Comparable<? super T> >

Why is T extends Comparable <? super T> 为什么T extends Comparable <? super T> T extends Comparable <? super T> including T , meaning including Comparable<T> and not just the super class hierarchy? T extends Comparable <? super T>包括T ,意思是包括Comparable<T>而不仅仅是超类层次结构? I am confused on the super keyword as I thought super would only include super class items. 我对super关键字感到困惑,因为我认为super只包含超类项。 I am new to java and in the Java book it had the following example: 我是java的新手,在Java书中它有以下示例:

This is regarding a method in a class that was trying to limit the upper and lower bound of the hierarchy using the Java.awt.Component so the class had extends Container 这是关于一个类中的方法,该类试图使用Java.awt.Component限制层次结构的上限和下限,因此该类扩展了Container

class CustomComponent<T extends Container>

in the class they have the following method 在课堂上他们有以下方法

void describeComponent<CustomComponent<? super JPasswordField> ref)

and then goes on to say 然后继续说

Note that JPasswordField, which is a superclass of JTextField, itself is omitted in the list of permissible objects. 请注意,JPasswordField是JTextField的超类,它本身在允许的对象列表中被省略。

From Lower Bounded Wildcards , a section of the Generics section of The Java Tutorial : Lower Bounded WildcardsThe Java TutorialGenerics部分的一部分:

... a lower bounded wildcard restricts the unknown type to be a specific type or a super type of that type. ... 下限有界通配符将未知类型限制为该类型的特定类型或类型。

(bold mine, emphasis theirs) (大胆的,强调他们的)

Thus, the set of classes that match T extends Comparable<T> is a subset of the set of classes that match T extends Comparable<? super T> 因此,匹配T extends Comparable<T>的类集合T extends Comparable<T>是匹配T extends Comparable<? super T>的类集合的子集T extends Comparable<? super T> T extends Comparable<? super T> . T extends Comparable<? super T> The wildcard ? super T 通配符? super T ? super T itself matches T and any superclasses of T . ? super T本身相匹配T和任何超T

In other words, the assumption that " super would only include super class items" is simply incorrect. 换句话说,“ super只包括超类项目”的假设是完全错误的。

Your confusion in the example may also arise from the fact that JTextField is a superclass of JPasswordField ; 您在示例中的混淆也可能源于JTextFieldJPasswordField超类 ; in other words, JPasswordField extends JTextField . 换句话说, JPasswordField extends JTextField The example would match any of the following classes: 该示例将匹配以下任何类:

  • javax.swing.JPasswordField javax.swing.JPasswordField中
  • javax.swing.JTextField javax.swing.JTextField中
  • javax.swing.JTextComponent javax.swing.JTextComponent
  • javax.swing.JComponent javax.swing.JComponent中
  • java.awt.Container java.awt.Container中
  • java.awt.Component java.awt.Component中
  • java.lang.Object java.lang.Object继承

The example would make much more sense as the following: 这个例子会更有意义,如下所示:

 void describeComponent(CustomComponent<? super JTextField> ref) {...} 

Note that JPasswordField, which is a subclass of JTextField, itself is omitted in the list of permissible objects, because it is not a superclass of JTextField. 请注意,JPasswordField是JTextField的子类,它本身在允许的对象列表中被省略,因为它不是JTextField的超类。

我相信“<T extends Comparable <?super T >>”的语义是T或T的超类必须实现Comparable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM