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