简体   繁体   English

java EnumSet,不兼容的类型:推断变量E具有不兼容的范围

[英]java EnumSet, incompatible types: inference variable E has incompatible bounds

I have a the below method which returns a enum set containing all of the elements of Types : 我有一个下面的方法,它返回一个包含Types所有元素的枚举集:

@Override
public EnumSet<?> groupTypes() {
    return EnumSet.allOf(Types.class);
}

And the Types is an enum like below: 并且Types是一个如下所示的enum

public enum Types implements GroupType {
    ASG;
}

The GroupType interface is: GroupType接口是:

 public interface GroupType extends NamedType {

 }

The NamedType interface: NamedType接口:

public interface NamedType {

    String name();

}

When compile, I got the below error: 编译时,出现以下错误:

    error: incompatible types: inference variable E has incompatible bounds
return EnumSet.allOf(Types.class);
                        ^

equality constraints: Types
    upper bounds: Enum<CAP#1>,Enum<E>
  where E is a type-variable:
    E extends Enum<E> declared in method <E>allOf(Class<E>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Enum<CAP#1> from capture of ?

The error 错误

Error: incompatible types: inference variable E has incompatible bounds return EnumSet.allOf(Types.class); 错误:不兼容的类型:推断变量E具有不兼容的范围return EnumSet.allOf(Types.class);

is telling you what is the problem. 告诉你是什么问题。 One way to resolve is to change the signature like this 解决的一种方法是像这样更改签名

public EnumSet<? extends NamedType> groupTypes() {
        return EnumSet.allOf(Types.class);
    }

or you can just assign EnumSet.allOf(Types.class) to a variable and then there is no need to change the method signature like this 或者您可以只将EnumSet.allOf(Types.class)分配给变量,然后无需像这样更改方法签名

public EnumSet<?> groupTypes() {
    EnumSet<Types> typeses = EnumSet.allOf(Types.class);
    return typeses;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:不兼容的类型:推断变量 RR 的边界不兼容 - java: incompatible types: inference variable RR has incompatible bounds Java “错误:不兼容的类型:推理变量 E 的边界不兼容”错误 - Java "error: incompatible types: inference variable E has incompatible bounds" error 不兼容的类型:推理变量E#1具有不兼容的上限Enum <E#2> - incompatible types: inference variable E#1 has incompatible upper bounds Enum<E#2> 错误:不兼容的类型:推断变量R具有不兼容的范围 - error: incompatible types: inference variable R has incompatible bounds 错误:不兼容的类型:推断变量R具有不兼容的界限(Lambda Java 8) - error: incompatible types: inference variable R has incompatible bounds (Lambda java 8) java:不兼容类型:推理变量T具有不兼容的边界等式约束:下限:java.util.List &lt;&gt; - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> java:不兼容的类型:推理变量 T 具有不兼容的边界等式约束:下限:java.util.List&lt;&gt; 用于添加字段 - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> for adding field 不兼容的类型:推理变量T具有不兼容的边界等式约束:捕获#1的? 扩展java.lang.Object - incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object Java 8编译错误“推理变量D具有不兼容的范围” - Java 8 compilation error “inference variable D has incompatible bounds” Java实体组件系统-推断变量T具有不兼容的范围 - Java Entity Component System - Inference variable T has incompatible bounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM