简体   繁体   English

Java-的含义 <T extends Comparable<T> &gt;?

[英]Java- The meaning of <T extends Comparable<T>>?

The full context being: 完整的上下文是:

public class RClass<T extends Comparable<T>>

Would I be right in saying that the statement in the title means that the arguments plugged into the method must either be an object of a class which implements Comparable OR one of its derived classes? 我说对了,标题中的语句意味着插入该方法的参数必须是实现Comparable的类的对象,或者是其派生类之一?

Thanks. 谢谢。

This means that the type parameter must support comparison with other instances of its own type, via the Comparable interface. 这意味着type参数必须通过Comparable接口支持与其自身类型的其他实例的比较。

An example of such a class is provided in the Oracle tutorial Object Ordering . Oracle教程“ 对象订购”中提供了此类的示例。 Note the similar pattern to T extends Comparable<T> in the excerpt below: 请注意,以下摘录中与T extends Comparable<T>类似的模式T extends Comparable<T>

public class Name implements Comparable<Name> {
   ...
   public int compareTo(Name n) { ... }
}

Java- The meaning of <T extends Comparable<T>> ? Java- <T extends Comparable<T>> 的含义

a) Comparable <T> is a generic interface (remember it's an "interface" ie not a "class") a) Comparable <T>是通用接口(请记住,它是一个“接口”即不是“类”)

b) extends means inheritance from a class or an interface. b) extends是指从类或接口继承。

From above-said point#a, it is an interface..(Remember it is an inheritance from an "interface" ie not from a "class") 从上述#a点开始,它是一个接口。(请记住,这是“接口”的继承,而不是“类”的继承)

c)From above-said both points #a & #b, c)从上述#a和#b两点出发,

here "one interface" extends "another interface". 这里的“一个接口”扩展了“另一个接口”。

There should be an interface defined for this class.. just an example here is 应该为该类定义一个接口..这里只是一个例子

interface MinMax<T extends Comparable<T>> { 
    T min(); 
    T max(); 
} 

d) now your class ie public class RClass {} SHOULD d)现在您的课程,即public class RClass {}

1# EITHER "implement" this "generic interface" Comparable<T> ..!!! 1#都“实现”此“通用接口” Comparable<T> .. !!!

ex: public class RClass<T> implements Comparable<T> 例如: public class RClass<T> implements Comparable<T>

2# OR create an interface and extend to this "generic interface" Comparable<T> ex: 2#或创建一个接口并扩展到此“通用接口” Comparable<T>例如:

interface MinMax<T extends Comparable<T>> { 
   T min(); 
   T max(); 
} 
class RClass<T extends Comparable<T>> implements MinMax<T> {
    .....
    .....
}

Here, Pay special attention to the way that the type parameter T is declared by RClass and then passed to MinMax . 在此,请特别注意RClass声明类型参数T并将其传递给MinMax Because MinMax requires a type that implements Comparable , the implementing class (RClass in this case) must specify the same bound. 因为MinMax需要实现Comparable的类型,所以实现类(在这种情况下为RClass)必须指定相同的界限。 Furthermore, once this bound has been established, there is no need to specify it again in the implements clause. 此外,一旦建立了此界限,就无需在Implements子句中再次指定它。

Somewhere in that class, the programmer needs to write something like 在该类的某个地方,程序员需要编写类似

if(t.compareTo(othert) < 0) {
    ...
}

For that to work, the type T must have a compareTo-method which compares it to another object of type T. Extending Comparable guarantees the existence of such a method, among other things. 为此,类型T必须具有一个compareTo-method,该方法将它与类型T的另一个对象进行比较。扩展Comparable保证了这种方法的存在。

Yes, and bear in mind that objects of classes derived from Comparable ARE Comparable objects. 是的,请记住,从Comparable派生的类的对象 Comparable对象。 Inheritance is a is-a relationship. 继承是is-a关系。

It means that you can only create an instance of RClass with a type which quite literally extends Comparable<T> . 这意味着您只能创建一个RClass实例,该实例的类型确实可以扩展Comparable<T> Thus, 从而,

RClass<Integer> a;

is acceptable, since Integer extends Comparable<Integer> , while 可以接受,因为Integer扩展了Comparable<Integer> ,而

RClass<Object> b;

is not, since Object is not a class which extends comparable at all. 不是,因为Object根本不是可扩展的类。

Simply put, the generic type T must be comparable in order to compareTo. 简而言之,泛型T必须具有可比较性才能进行compareTo。 otherwise you cannot do T.compareTo. 否则,您将无法执行T.compareTo。 In Item 28 Effective java, it suggests: "always use Comparable<? super T> in preference to Comparable<T>. <T extends Comparable<? super T>>" 在项目28有效的Java中,它建议: "always use Comparable<? super T> in preference to Comparable<T>. <T extends Comparable<? super T>>"

==>guhanvj, <T extends Comparable<T>> means that <T> is having upper bound of Comparable<T> objects. ==> guhanvj, <T extends Comparable<T>>表示<T>具有Comparable<T>对象的上限。 So <T> can have types of Byte, Character, Double, Float, Long, Short, String, and Integer classes which all implements Comparable<T> interface for natural ordering. 因此, <T>可以具有Byte,Character,Double,Float,Long,Short,String和Integer类的类型,它们均实现Comparable<T>接口以实现自然排序。

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

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