我有一个界面
public interface TerminalSymbol {
// methods ...
}
一个枚举
// common usage enum that I need
public enum Common implements TerminalSymbol {
EPSILON;
@Override
// methods ...
}
我想做类似的事情:
enum Term implements TerminalSymbol {
A, B, C, ...
@Override
// methods ...
}
EnumSet<? extends TerminalSymbol> terminalSymbols = EnumSet.allOf(Term.class);
terminalSymbol.add(Common.EPSILON); // this line gives me error
这个错误是(在我的情况下):
The method add(capture#1-of ? extends TerminalSymbol) in the type AbstractCollection<capture#1-of ? extends TerminalSymbol> is not applicable for the arguments (Common)
现在我知道如果我使用Set<SomeInterface>
我可以防止这种类型的错误(并且我可以继续开发代表正式语法的类)但我想使用它,EnumSet
因为它可能比HashSet
. 我怎么解决这个问题?