[英]Java Comparable for Generic Types
When trying to compare a generic type in the form 尝试比较表单中的泛型类型时
Class<T> implements Comparable<T> {
public int compareTo(T other){
this.T < other
}
}
does not work for me but when using 对我不起作用,但是在使用时
Class<T extends Comparable<T>>{
T.compareTo(other.T)
}
does work. 确实有效。 I have been unable to deciper why I can't compare T directly using the first example 我无法用第一个例子解释为什么我不能直接比较T
In your first example: 在第一个示例中:
class Foo<T> implements Comparable<T> {
you're saying that Foo
objects are comparable. 您是说Foo
对象具有可比性。 In your second example: 在第二个示例中:
class Foo<T extends Comparable<T>>{
you're saying that whatever T
, is, it's comparable. 您是说无论T
是可比的。
Then, in the body of your code, you try to compare things of type T
-- in the first case, you have no guarantee that they're comparable, in the second, you do. 然后,在代码正文中,您尝试比较T
类型的事物-在第一种情况下,您不能保证它们具有可比性,在第二种情况下,您可以做得到。
I hope these two exmaples will cast some light on your problem: 我希望这两个例子可以为您的问题提供一些启示:
class Foo<T> implements Comparable<T> {
@Override
public int compareTo(T o) {
// We don't know anything about T
return hashCode() - o.hashCode();
}
}
class Boo<T extends Comparable<? super T>> implements Comparable<T> {
private T instance;
@Override
public int compareTo(T o) {
// We know that T implements Comparable<? super T>
return instance.compareTo(o);
}
}
In first case with Foo
, you don't know anything about type T
, so you can't do much in your compareTo()
method. 在Foo
第一种情况下,您对T
类型一无所知,因此您不能在compareTo()
方法中做太多事情。
However, in Boo
, T
is required to implement Comparable<? super T>
但是,在Boo
,需要T
来实现Comparable<? super T>
Comparable<? super T>
(if you don't know what wildcards are, just think there is simply Comparable<T>
), so you can call t.compareTo(anotherT)
. Comparable<? super T>
(如果您不知道通配符是什么,只需认为那里只是Comparable<T>
),因此可以调用t.compareTo(anotherT)
。 More about bounded type parameters . 有关有界类型参数的更多信息。
EDIT: (wildard explained) 编辑:(对野生动物的解释)
Consider following code: 考虑以下代码:
class Car implements Comparable<Car> { ... }
class SportCar extends Car { ... }
Now call sportCar1.compareTo(SportCar2)
is perfectly legal. 现在调用sportCar1.compareTo(SportCar2)
是完全合法的。 However, without the wildcard, Bar<SportCar>
is a cpompile error! 但是,没有通配符, Bar<SportCar>
是一个错误!
Why? 为什么? Because SportCar
doesn't implement Comparable<SportCar>
. 因为SportCar
没有实现Comparable<SportCar>
。 And you require T
to implement Comparable<T>
, and in this case T
is SportCar
. 并且您需要T
来实现Comparable<T>
,在这种情况下, T
是SportCar
。
But SportCar
implements Comparable<Car>
and Car
is a supertype of SportCar
. 但是SportCar
实现Comparable<Car>
而Car
是SportCar
的超类型。 So you want to say something like "T can be compared to T or any supertype of T" (like in this case SportCar
can be compared to any Car
). 因此,您想说些类似“可以将T与T或任何T的超类型进行比较”(例如,在这种情况下, SportCar
可以与任何Car
进行比较)。
And that what the wildcard is for (among many other things). 这就是通配符的用途(在许多其他事情中)。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.