这个类声明在Java中是什么意思?

这个类声明在Java中是什么意思?,java,generics,Java,Generics,我只是学习到了tree,但有一件事我不明白,那就是类声明: 例如:classBinarySearchTree这声明了一个具有单个泛型类型参数的类。因为对于二进制搜索树,它必须能够比较两个项目,所以需要指定这一点,以便编译器能够验证它 尖括号中的部分是类型参数T,它的一个约束表示: 无论T是什么,它都应该扩展compariable() SaidComparable应该能够将自己与T或T的超类进行比较(Sun's是开始学习通配符和泛型的好地方 对于java“Grand HONNS”也很好。 < P

我只是学习到了tree,但有一件事我不明白,那就是类声明:


例如:class
BinarySearchTree
这声明了一个具有单个泛型类型参数的类。因为对于二进制搜索树,它必须能够比较两个项目,所以需要指定这一点,以便编译器能够验证它

尖括号中的部分是类型参数
T
,它的一个约束表示:

  • 无论
    T
    是什么,它都应该扩展
    compariable
  • Said
    Comparable
    应该能够将自己与
    T
    T
    的超类进行比较(
    Sun's是开始学习通配符和泛型的好地方

    对于java“Grand HONNS”也很好。

    < P>括号是用于泛型的。这是一个C++中的模板,并让你创建一个可以强类型化的数据结构。例如,AARAYLIST对象使用一个泛型来定义数组中的哪些类型:

    ArrayList<String> - an ArrayList containing Strings
    ArrayList<MyClass> - an ArrayList containing MyClass objects
    
    ArrayList-包含字符串的ArrayList
    ArrayList-包含MyClass对象的ArrayList
    
    当您定义一个使用泛型的结构时,您可以使用上面的符号。“T”是某个类的占位符,该类在实例化并给定类型时填充。例如,ArrayList的定义可能如下所示:

    public class ArrayList<T> ...
    
    公共类ArrayList。。。
    
    最简单的方法是简单地使用
    MyGeneric
    并允许使用任何类。但是,有时您只希望gereric与特定继承结构中的类一起使用。在这种特定情况下,短语
    Comparable
    
    // 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
    }
    
    class Helper<T extends Comparable<? super T>> {
         // some helper methods
    }