简体   繁体   English

java通用可比成语

[英]java generic comparable idiom

I encountered the following piece of a definition for a generic class: 我遇到了泛型类的以下定义:

public class binarysearchnode<T extends Comparable<T>> implements Comparable<binarysearchnode<T>>{
.............
}

Please help explaining why a class would specify itself as a Type parameter to comparable while implementing the comparable interface? 请帮助解释为什么类在实现类似的接口时将自己指定为Type参数以进行比较? How would it be different from the following: 它与以下内容有何不同:

public class binarysearchnode<T extends Comparable<T>> implements Comparable<? super (or extends)T>{
.............
}

This makes it possible to compare binarysearchnode s to each other. 这使得可以将binarysearchnode彼此进行比较。 If it implemented Comparable<T> , that would instead mean that the node could be compared to the value of the node, which would be odd. 如果它实现了Comparable<T> ,那么这意味着可以将节点与节点的进行比较,这将是奇数。

Inside the class you will probably find something like this: 在课堂上你可能会发现这样的事情:

T value;

public int compareTo(binarysearchnode<T> other) {
   return value.compareTo(other.value);
}

In order to be able to implement compareTo() like this, the value class ( T ) needs to be comparable to other objects of its class - hence the declaration of <T extends Comparable<T>> in the class definition. 为了能够像这样实现compareTo() ,值类( T )需要与其类的其他对象相当 - 因此<T extends Comparable<T>>的声明在类定义中<T extends Comparable<T>>

It is because what the class author wants is to be able to write: 这是因为班级作者想要的是能够写:

b1.compareTo(b2)

where b1 and b2 are binarysearchnode instances. 其中b1b2binarysearchnode实例。 The developer also adds a constraint to T so that T extends Comparable<T> . 开发人员还T添加约束,以便T扩展Comparable<T> Probably so that the implementation of Comparable for binarysearchnode can just rely on T instances being Comparable themselves. 可能是因为binarysearchnode搜索binarysearchnodeComparablebinarysearchnode可以依赖于T实例本身就是Comparable

More generally, while it is possible for a class C1 to implement Comparable<C2> , ultimately, it makes no sense to do so: this does not mean that an instance of C2 could be comparable to an instance of C1 . 更一般地说,虽然C1类可以实现Comparable<C2> ,但最终这样做没有任何意义:这并不意味着C2的实例可以与C1的实例相比。 And due to type erasure, it would not be possible, for instance, for class C1 to implement both Comparable<C1> and Comparable<C2> . 并且由于类型擦除,这将是不可能的,例如,为类C1 同时实现Comparable<C1>Comparable<C2>

Also, if binarysearchnode<T> were to implement Comparable<T> directly, you would have at least two problems: 另外,如果binarysearchnode<T>直接实现Comparable<T> ,那么至少会有两个问题:

  • you would not be able to compare one binarysearchnode<T> to another; 你将无法将一个binarysearchnode<T>与另一个进行比较;
  • given a binarysearchnote<T> instance b and a T instance t , you would be able to write b.compareTo(t) but not t.compareTo(b) (since T does not, and cannot, implement Comparable<binarysearchnode<T>> ), and that breaks the Comparable contract. 给定一个binarysearchnote<T>实例b和一个T实例t ,你可以写b.compareTo(t)而不是t.compareTo(b) (因为T没有,也不能,实现Comparable<binarysearchnode<T>> ),这打破了Comparable合同。

Let's say you have a superclass A and a subclass B . 假设你有一个超类A和一个子类B Imagine the superclass implements Comparable<A> , then B will also implement Comparable<A> through inheritance. 想象一下,超类实现了Comparable<A> ,然后B也将通过继承实现Comparable<A>

Your binarysearchnode class declared as such : 您的binarysearchnode类声明为:

public class binarysearchnode<T extends Comparable<T>>

will not be able to take B as a type parameter for T ( B does not implement Comparable<B> ) But when defined as such : 将无法将B作为T的类型参数( B未实现Comparable<B> )但是当定义为:

public class binarysearchnode<T extends Comparable<? super T>>

it will be able to take B as a type parameter for T , since B implements Comparable<A> which fulfills Comparable<? super T> 它可以将B作为T的类型参数,因为B实现了Comparable<A> ,它实现了Comparable<? super T> Comparable<? super T> . Comparable<? super T>

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

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