我有一个类和函数定义如下:
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);
}
}
对于这两个 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 ?>)
本质上,我需要某种形式的 Enum
泛型,而我只是对似乎存在的限制感到沮丧。我有许多不兼容的 Enum
类型,但我需要一些可以容纳任何类型的容器。
我认识到,在这段代码中,无法确定 a
、b
和 contents
都是相同的 Enum
类型,但是在实际实现中这应该没有问题。
解决我的一般 Enum
问题的任何想法和可能的方法都是有值(value)的,我将非常感谢您的帮助。提前致谢。
最佳答案
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);
}
}
关于java - 通用枚举/EnumSet 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545267/