简体   繁体   English

Java泛型方法结构说明

[英]Java Generics method structure explanation

I trying to learn java generic, from here I got below method definition, in this can someone explain why we are declaring <T extends Comparable<T>> before the return type. 我试图学习java泛型,从这里我得到了下面的方法定义,可以有人解释为什么我们在返回类型之前声明<T extends Comparable<T>>

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem)

I know the usage of Comparable interface, but why we need this <T extends Comparable<T>> before the return type of the method? 我知道Comparable接口的用法,但是为什么我们需要在方法的返回类型之前使用此<T extends Comparable<T>>

Rather we can write like public static int countGreaterThan(T[] anArray, T elem) , which will also take generic parameters 相反,我们可以像写public static int countGreaterThan(T[] anArray, T elem) ,它也将采用通用参数

So my question is Why we need <T extends Comparable<T>> or just a <T> there? 所以我的问题是,为什么我们需要<T extends Comparable<T>>或只是一个<T>

It simply means the generic type T must extend Comparable<T> . 这仅表示通用类型T必须扩展Comparable<T> By telling the compiler that, your T objects will have all the public methods of Comparable<T> available to it. 通过告诉编译器,您的T对象将可以使用Comparable<T>所有公共方法。

Comparable<T> is an interface whose contract means "we can compare this object to one of type T". Comparable<T>是一个接口,其协定表示“我们可以将此对象与T类型之一进行比较”。 If the object itself is of that type T, then the object is comparable to itself. 如果对象本身的类型为T,则该对象与其自身具有可比性。 This is the behavior that we desire here. 这是我们在这里想要的行为。 Examples of similar behavior are Integer which implements Comparable<Integer> . 类似的行为的例子是Integer ,它实现Comparable<Integer>

Here, this method requires that you pass in objects that are comparable to themselves. 在此,此方法要求您传入与自己可比较的对象。

If those objects are of type T then they must implement Comparable<T> in order to be comparable with each other. 如果这些对象的类型为T,则它们必须实现Comparable<T>以便彼此比较。 Since generic extends/implements constraints use the keyword extends in all cases, the generic declaration is T extends Comparable<T> . 由于泛型扩展/实现约束在所有情况下都使用关键字extends ,因此泛型声明为T extends Comparable<T>

The method takes an array of items and a single item, and I guess from the name that it will return the number of items in anArray that are "greater than" elem . 该方法需要一个项目数组和一个项目,并且从名称来看,我猜想它将返回anArray中“大于” elem

This will work for any type T that has a well-defined "greater than" relation, ie any type that is Comparable to itself, which is precisely what <T extends Comparable<T>> expresses - any type T so long as that type is an implementation of Comparable with the target type itself as its type parameter. 这将适用于具有明确的“大于”关系的任何类型T ,即,任何与自身Comparable类型,正是<T extends Comparable<T>>表示的形式-任何类型T ,只要该类型是将目标类型本身作为其类型参数的Comparable的实现。

To be able to access: compareTo(T o); 能够访问: compareTo(T o); we must extend the Comparable interface . 我们必须扩展Comparable接口

Why, because only when we do that can we access it, as it isn't available beforehand. 为什么,因为只有在执行此操作后,我们才能访问它,因为它事先无法使用。

在此处输入图片说明

Wikipedia has an excellent article on Generics in Java. Wikipedia上有一篇关于Java泛型的出色文章

它限制T必须是实现Comparable的类,以便T的一个实例可以与T的另一个实例进行比较

countGreaterThan method is a generic method that requires to compare an object with another object of its type. countGreaterThan方法是一种通用方法,需要将一个对象与该类型的另一个对象进行比较。 A compareTo ( of the Comparable interface ) method provides the types implementing Comparable interface with a way to compare two objects of that type. CompareTo(Comparable接口的)方法为实现Comparable接口的类型提供一种比较该类型的两个对象的方式。

Since countGreaterThan method makes use of the compareTo method, it is required that the arguments supplied to the method are of those types that implement the Comparable interface. 由于countGreaterThan方法使用compareTo方法,因此要求提供给该方法的参数具有实现Comparable接口的那些类型。

When you declare a bounded type parameter, you're telling the compiler that T is a type that implements the Comparable interface. 当您声明一个有界的类型参数时,您将告诉编译器T是实现Comparable接口的类型。 Hence it is safe to invoke the compareTo method on the objects of type T. So declaring the bounded type as > lets you invoke the compareTo method on the object of the type T. Now the compiler will let you invoke the compareTo method on the objects of the generic type. 因此,在类型T的对象上调用compareTo方法是安全的。因此,将有界类型声明为>可使您在类型T的对象上调用compareTo方法。现在,编译器将让您在对象上调用compareTo方法。通用类型。 Not only that, the compiler will also enforce that the type of the arguments supplied to the method countGreaterThan implement the Comparable interface. 不仅如此,编译器还将强制要求提供给方法countGreaterThan的参数类型实现Comparable接口。

T means any type. T表示任何类型。 So if you have the method 所以如果你有方法

public static int countGreaterThan(T[] anArray, T elem)

that means you can put call it like this: 这意味着您可以这样称呼它:

public static int countGreaterThan(String[] anArray, String elem)

or with any other class. 或任何其他班级。 The only constraint you have here is that both classes represented by T are the same. 您在这里的唯一约束是T表示的两个类是相同的。

The > means, that T needs to be a subclass of Comparable. >表示T必须是Comparable的子类。 So T needs to implement Comparable. 因此,T需要实现可比性。 An example would be Integer, which implements Comparable. 一个示例是Integer,它实现Comparable。 That means, Integer has the method Integer.compareTo(Integer i). 这意味着,Integer具有方法Integer.compareTo(Integer i)。

The > constraint might be usefull if you want to use certain methods that are defined by that interface. 如果要使用该接口定义的某些方法,>约束可能很有用。 For your method countGreaterThan(): this one needs to use .compareTo() to figure out if an input-object is greater than the other one, so every input-object needs to have the .compareTo()-method. 对于您的方法countGreaterThan():此方法需要使用.compareTo()来确定一个输入对象是否大于另一个对象,因此每个输入对象都需要具有.compareTo()方法。 So you declare > and now you can use .compareTo(). 因此,您声明>,现在可以使用.compareTo()。

Since this is a static function, information about T has to accompany it. 由于这是静态函数,因此必须附带有关T的信息。 For instance methods, T will already be defined/constrained. 对于实例方法,T将已经被定义/约束。 Ie: 即:

class Example<T extends Comparable<T>> {
    public void doSomethingWithT(T param) { ... };
}

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

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