泛型边界与pre-generics原始类型 [英] Generic bounds with pre-generics raw types
问题描述
我有一个元素类型为< K extends Comparable< K>>
的集合,因为它是有序的。
I've got a Collection with the element type of <K extends Comparable<K>>
because it's ordered.
我试图在这个集合中使用Joda LocalTime
- 例如 MyCollection< LocalTime>
。不幸的是,显然Joda是预典; LocalTime
实现原始可比较
。
I'm trying to use Joda LocalTime
with this collection - e.g. MyCollection<LocalTime>
. Unfortunately, apparently Joda is pre-generics; LocalTime
implements raw Comparable
.
编译器错误
Bound mismatch: The type LocalTime is not a valid substitute
for the bounded parameter <K extends Comparable<K>> of the type Interval<K>
我希望这只是一个警告,因为它很可能分配一个原始类型一个类型化的变量。
I would expect this to work with just a warning, as it's quite possible to assign a raw type to a typed variable.
为什么这不工作,我能做些什么呢?
Why doesn't this work, and what can I do about it?
最小代码示例:
class Holder<K extends Comparable<K>> { }
class WTF extends Holder<LocalTime> { }
推荐答案
c $ c> LocalTime 其中实现可比较的< K>
并通过所有方法调用。
You could create a wrapper class around LocalTime
which implements Comparable<K>
and passes through all the method calls.
这篇关于泛型边界与pre-generics原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!