Java 泛型绑定与原始类型不匹配
我有一个带有类似签名的超类
public abstract class Foo<C extends Comparable<? super C>>{..}
,所以 C 类应该是一个 Comparable 对象。
我想使用 org.joda.time.Instant (版本 1.6)作为子类中的类型参数,
public class SubFoo<Instant>
不幸的是我收到此错误:
Bound mismatch: The type Instant is not a valid substitute for the bounded parameter <C extends Comparable<? super C>> of the type BigtablePoller<T,C>
Instant 类扩展了 AbstractInstant ,它实现了 Comparable (无类型参数)
有没有办法解决这个问题?
我能够使其工作的唯一方法是将 Foo 更改为:
@SuppressWarnings("rawtypes")
public abstract class Foo<C extends Comparable> {}
我想避免警告,并且 Josh Bloch 说不要在新代码中使用原始类型(有效的 java Item 23)。
I have a super class with a signature like
public abstract class Foo<C extends Comparable<? super C>>{..}
So the C class is supposed to be a Comparable object.
I want to use org.joda.time.Instant (version 1.6) as the type parameter in a subclass
public class SubFoo<Instant>
unfortunately i get this error:
Bound mismatch: The type Instant is not a valid substitute for the bounded parameter <C extends Comparable<? super C>> of the type BigtablePoller<T,C>
The Instant class extends AbstractInstant which implements Comparable (no type parameters)
Is there any way around this issue?
The only way i have been able to make it work is to change Foo to:
@SuppressWarnings("rawtypes")
public abstract class Foo<C extends Comparable> {}
I would like to avoid the warning and Josh Bloch says not to use raw types in new code (effective java Item 23).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用这个:
您的版本不起作用的原因是
Instant
被定义为extends Comparable
而不是extends Comparable<?超级即时>
。如果您使用边界,则边界也必须匹配。Just use this:
The reason your version doesn't work is that
Instant
is defined asextends Comparable<Instant>
notextends Comparable<? super Instant>
. If you use bounding, the bounding has to match too.Joda time 1.6 不支持泛型,因此有三个选项。
Joda time 1.6 doesn't support Generics so there are three options.