我正在尝试在以我自己的 Enum 作为参数的抽象类中创建一个抽象方法。但我也希望 Enum 是通用的。
所以我这样声明:
public abstract <T extends Enum<T>> void test(Enum<T> command);
在实现中,我将枚举作为那个:
public enum PerspectiveCommands {
PERSPECTIVE
}
并且方法声明变为:
@Override
public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) {
}
但如果我这样做:
@Override
public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) {
if(command == PerspectiveCommands.PERSPECTIVE){
//do something
}
}
我无权访问PerspectiveCommands.PERSPECTIVE
错误:
cannot find symbol symbol: variable PERSPECTIVE location: class Enum<PerspectiveCommands> where PerspectiveCommands is a type-variable: PerspectiveCommands extends Enum<PerspectiveCommands> declared in method <PerspectiveCommands>test(Enum<PerspectiveCommands>)
我做了一个这样的解决方法:
public <T extends Enum<T>> byte[] executeCommand(Enum<T> command) throws Exception{
return executeCommand(command.name());
}
@Override
protected byte[] executeCommand(String e) throws Exception{
switch(PerspectiveCommands.valueOf(e)){
case PERSPECTIVE:
return executeCommand(getPerspectiveCommandArray());
default:
return null;
}
}
但我想知道是否可以不通过我的解决方法?