简体   繁体   English

为什么“类型绑定不匹配:类型? 扩展T不是有界参数的有效替代品 <E extends Enum<E> &gt;枚举类型 <E> “?

[英]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.

相关问题 有界不匹配:类型不是有界参数的有效替代品<e extends comparable<e> &gt; 类型</e> - Bound mismatch: The type is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type Java Generics - 绑定不匹配:类型 Object 不是绑定参数的有效替代品<e extends comparable<e> &gt;</e> - Java Generics - Bound mismatch: The type Object is not a valid substitute for the bounded parameter <E extends Comparable<E>> JAVA:绑定不匹配:不是有界参数的有效替代 <E extends Comparable<E> &gt; - JAVA : Bound mismatch: is not a valid substitute for the bounded parameter <E extends Comparable<E>> 绑定不匹配:MyClass1类型不是绑定参数的有效替代品 <T extends Comparator<T> &gt;类型的人 <T> - Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T> 边界不匹配:类型A不是边界参数的有效替代品 <T extends Entity> 类型为TestService <T> - Bound mismatch: The type A is not a valid substitute for the bounded parameter <T extends Entity> of the type TestService<T> 绑定不匹配:该类型不是绑定参数的有效替代 - Bound mismatch: The type is not a valid substitute for the bounded parameter 最简单的方法来转换类 <T> 到一个班级 <E extends Enum<E> &gt;不丢失类型信息 - Simplest way to cast a Class<T> to a Class<E extends Enum<E>> without losing type information Java泛型:绑定不匹配:类型不是该类型的bounded参数的有效替代 - Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type l如何从泛型类型的实例调用values() <E extends Enum<E> &gt;? - How l can call values() from instance of generic type <E extends Enum<E>>? 类型 T 不是有界参数的有效替代品 ` <t extends collection<?> &gt;</t> - Type T is not a valide Substitute for the bounded Parameter `<T extends Collection<?>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM