[英]Bound mismatch: The type is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type
[英]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>“?
我正在嘗試在Eclipse中閱讀hadoop 1.0.0源代碼。 我先下載了源代碼,然后使用ant eclipse
構建了項目。 之后,我在Eclipse中成功創建了項目。 但是有一個錯誤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>
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>
。 錯誤代碼:
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();
}
枚舉本身是通用的(在純Java中),對參數T
有限制,因此:
`Enum<T extends <Enum<T>>`
您對代碼中的T沒有任何限制,因此編譯器會抱怨,因為您可能會以T = Object
結束,但Enum
無法接受Object
作為T
因此,您還必須在代碼中限制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();
}
實際上,在這種情況下,通配符沒有意義,因為您不能擴展T
(因為您不能擴展任何特定的enum
)。 但這應該已經編譯。 如果不是,請刪除通配符。
我知道,這不是您的代碼。 因此,可能是一些較舊的Java無法正確檢查此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.