简体   繁体   English

这个类声明在Java中意味着什么?

[英]What does this class declaration mean in Java?

I just learn up to tree and one thing I don't understand about it is the class declaration: 我只是学习树和一件我不明白的事情是类声明:

for example: class BinarySearchTree<T extends Comparable<? super T>> 例如:class BinarySearchTree<T extends Comparable<? super T>> BinarySearchTree<T extends Comparable<? super T>> . BinarySearchTree<T extends Comparable<? super T>>

Now, can you please explain me what is in the bracket and the " <? super T> "? 现在,请您解释一下括号中的内容和“ <? super T> ”?

Any good source you can refer me to? 你能引用我的任何好消息来源吗? Thanks. 谢谢。

This declares a class with a single generic type parameter. 这声明了一个具有单个泛型类型参数的类。 Since for the binary search tree it's imperative that it can compare two items, this needs to be specified so that the compiler can verify it. 因为对于二叉搜索树,它必须比较两个项,这需要指定,以便编译器可以验证它。

The part in angle brackets is the type parameter T and a constraint for it which says: 尖括号中的部分是类型参数T和它的约束,它表示:

  • Whatever T is, it should extend Comparable ( <T extends Comparable<...>> ). 无论T是什么,它都应该扩展Comparable<T extends Comparable<...>> )。
  • Said Comparable should be able to compare itself to either T or a super-class of T ( <? super T> ). 所述Comparable应该能够将自身与T或超级T<? super T> )进行比较。

Since T could usually be anything this constrains the choice to types where it makes sense to implement a search tree for. 由于T通常可以是任何东西,因此限制了对实现搜索树有意义的类型的选择。

<? super T> <? super T> means that ? <? super T>意味着? is the parent of T . T的父母。 If ? 如果? is known (for example X ), you can use <T extends X> 已知(例如X ),您可以使用<T extends X>

EDIT : This is good source for Java Generic: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf 编辑 :这是Java Generic的良好来源: http//java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

The <> brackets are for what's called generics. <>括号用于所谓的泛型。 This is alot like a template in C++ and alows you to create a single data structure that can be strongly typed. 这很像C ++中的模板,并且您可以创建一个可以强类型化的单个数据结构。 For example, the ArrayList object uses a generic to define what type of items are in the ArrayList: 例如,ArrayList对象使用泛型来定义ArrayList中的项类型:

ArrayList<String> - an ArrayList containing Strings
ArrayList<MyClass> - an ArrayList containing MyClass objects

When you are defining a structure that makes use of generics you use to notation above. 当您定义使用泛型的结构时,您可以使用上面的表示法。 The "T" is a placeholder for some class that is filled in when the class is instantiated and given a type. “T”是某个类的占位符,在实例化类并给出类型时填充该类。 For example, the definition of ArrayList might look something like this: 例如,ArrayList的定义可能如下所示:

public class ArrayList<T> ...

The simplest way is to simply use MyGeneric<T> and let any class be used. 最简单的方法是简单地使用MyGeneric<T>并让任何类使用。 However, sometime you only want the gereric to be used with classes in a certain inheritence structure. 但是,有时您只希望gereric与某个继承结构中的类一起使用。 In this specific case Comparable<? super T> 在这个具体案例中Comparable<? super T> Comparable<? super T> means that this is going to be an object that extend Comparable the Compares any type of object that matches T or is a super-class of T. Comparable<? super T>意味着这将是一个扩展Comparable的对象Compares匹配T的任何类型的对象或者是T的超类。

Sun's Tutorial is a good place to start learning about wildcards and generics. Sun的教程是开始学习通配符和泛型的好地方。

Java Ranch is also very good for Java "Greenhorns". Java Ranch也非常适合Java“Greenhorns”。

The phrase <? super T> 短语<? super T> <? super T> is a wildcard and implies that ,class BinarySearchTree can take: <? super T>是一个通配符,暗示BinarySearchTree类可以采用:
a. 一种。 a type parameter (T) that extends Comparable 一个扩展Comparable的类型参数(T)
b. and also can take a subtype (S) whose parent extends Comparable 并且还可以采用其父扩展Comparable的子类型(S)

The construct <? super T> 构造<? super T> <? super T> extends utility of class BinarySearchTree to type(s) that implements Comparable and its subtypes. <? super T>将类BinarySearchTree的实用程序扩展为实现Comparable及其子类型的类型。

Below code snippet demonstrates this: 下面的代码片段演示了这一点:

 // Below declaration of Helper class doesn't uses the wildcard super class Helper<T extends Comparable<T>> { // some helper methods } abstract class Animal implements Comparable<Animal> { public int compareTo(final Animal o) { // implementation ... } // other abstract methods } class Mammal extends Animal { // implement abstract methods } 

With the above declaration the statement Helper<Animal> x = new Helper<Animal>() works fine. 使用上面的声明,语句Helper<Animal> x = new Helper<Animal>()工作正常。
But the statement: Helper<Mammal> x = new Helper<Mammal>() gives the compile error 但声明: Helper<Mammal> x = new Helper<Mammal>()给出了编译错误
type parameter Mammal is not within its bound 类型参数Mammal不在其范围内
(the compiler version is javac 1.5.0_06) (编译器版本是javac 1.5.0_06)

When the declaration of the class Helper is changed to below form: 当类Helper的声明更改为以下形式时:

 class Helper<T extends Comparable<? super T>> { // some helper methods } 

then the statement Helper<Mammal> x = new Helper<Mammal>() doesn't give any compiler error. 那么语句Helper<Mammal> x = new Helper<Mammal>()不会给出任何编译器错误。

Thus usage of wildcard maximizes the utility of the class. 因此,使用通配符可以最大化该类的实用性。

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

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