这可能只是只见树木不见森林,但我有一个这样的类(class):
class MyClass<T extends Identifiable<?, String> & Displayable> {
// stuff
}
然后是另一个这样的:
class Value<I> implements Displayable, Identifiable<I, String> {
// other stuff
}
我假设MyClass<Value>
会编译,因为 Value
的所有实例实现 Displayable
和Identifiable<?, String>
,但事实并非如此:
Bound mismatch: The type Value is not a valid substitute for the bounded parameter <T extends Identifiable<?,String> & Displayable> of the type MyClass
即使我不使用 implements
中的类型我得到同样的错误:
class Value<I> implements Displayable, Identifiable<Long, String> {
// other stuff
}
当我更改 Value
时一切正常像这样的类:
class Value implements Displayable, Identifiable<Long, String> {
// other stuff
}
但我的问题是:为什么替换不能用于输入 Value
类(class)?
最佳答案
I assumed MyClass would compile
不,但是MyClass<Value<?>>
将编译。
Value<I>
是一个泛型类型。当省略类型变量 <I>
时你使用原始类型。当您使用原始类型时,类型变量 I
的Value
Value implements Identifiable<I, String>
不存在,编译器无法检查是否存在是 T extends Identifiable<?, String>
的有效替代品。因此编译器错误。
关于java - 泛型类型的绑定(bind)不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972871/