[英]Why ”Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>“?
I am trying to read the hadoop 1.0.0 source code in eclipse. 我正在尝试在Eclipse中阅读hadoop 1.0.0源代码。 I downloaded the source code first and then use
ant eclipse
to build the project. 我先下载了源代码,然后使用
ant eclipse
构建了项目。 After that, I successfully created the project in eclipse. 之后,我在Eclipse中成功创建了项目。 But there is an error
Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
但是有一个错误
Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
on the line 396 of Gridmix.java
. Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
Gridmix.java
的第396行上Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
Gridmix.java
Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>
。 The error code: 错误代码:
private <T> String getEnumValues(Enum<? extends T>[] e) {
StringBuilder sb = new StringBuilder();
String sep = "";
for (Enum<? extends T> v : e) {
sb.append(sep);
sb.append(v.name());
sep = "|";
}
return sb.toString();
}
Enum itself is generic (in pure Java) with restriction on the parameter T
so: 枚举本身是通用的(在纯Java中),对参数
T
有限制,因此:
`Enum<T extends <Enum<T>>`
You don't have any restriction on T in your code, so compiler complains, because You may end up with T = Object
, but Enum
cannot accept Object
as T
. 您对代码中的T没有任何限制,因此编译器会抱怨,因为您可能会以
T = Object
结束,但Enum
无法接受Object
作为T
So you have to restrict T
in your code as well: 因此,您还必须在代码中限制
T
:
private <T extends Enum<T>> String getEnumValues(Enum<? extends T>[] e) {
StringBuilder sb = new StringBuilder();
String sep = "";
for (Enum<? extends T> v : e) {
sb.append(sep);
sb.append(v.name());
sep = "|";
}
return sb.toString();
}
In fact in this case the wildcard wouldn't make sense, because you cannot extend T
(because you cannot extend any specific enum
). 实际上,在这种情况下,通配符没有意义,因为您不能扩展
T
(因为您不能扩展任何特定的enum
)。 But that should already compile. 但这应该已经编译。 If not, drop the wildcard.
如果不是,请删除通配符。
I see, it's not your code. 我知道,这不是您的代码。 So probably some older Java didn't check this properly.
因此,可能是一些较旧的Java无法正确检查此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.