[英]Generic Enum/EnumSet Troubles
I have a class and function defined as the following: 我有一个类和函数定义如下:
public class Site {
public EnumSet<?> contents;
public void determineStates(Site a, Site b) {
this.contents.clear();
this.contents.addAll(a.contents);
this.contents.addAll(b.contents);
a.contents.removeAll(b.contents);
this.contents.removeAll(a.contents);
}
}
For both addAll operations, Eclipse is giving me the following error message: 对于这两个addAll操作,Eclipse都给出了以下错误消息:
The method addAll(Collection<? extends capture#6-of ?>) in the type AbstractCollection<capture#6-of ?> is not applicable for the arguments (EnumSet<capture#7-of ?>)
Essentially I need some form of Enum
generics and I have just been frustrated with what appear to be limitations. 基本上我需要某种形式的
Enum
泛型,我只是对看似有限的东西感到沮丧。 I have a number of Enum
types that are incompatible but I desire some container that can hold any type. 我有许多不兼容的
Enum
类型,但我希望有一些容器可以容纳任何类型。
I recognize that in this code there is no way to be certain that a
, b
, and contents
are all of the same Enum
type, however in actual implementation this should not be problematic. 我认识到在这段代码中无法确定
a
, b
和contents
是否都是相同的Enum
类型,但在实际实现中这不应该是有问题的。
Any thoughts and possible methods to address my generic Enum
problem would be valuable and I would be immensely grateful for your help. 解决我的通用
Enum
问题的任何想法和可能的方法Enum
有价值,我非常感谢你的帮助。 Thanks in advance. 提前致谢。
The Site class should be generic: Site类应该是通用的:
public class Site<E extends Enum<E>> {
public EnumSet<E> contents;
public void determineStates(Site<E> a, Site<E> b) {
this.contents.clear();
this.contents.addAll(a.contents);
this.contents.addAll(b.contents);
a.contents.removeAll(b.contents);
this.contents.removeAll(a.contents);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.