Gar*_*ien 25 java generics methods enums
我正在尝试在抽象类中创建一个抽象方法,该方法将我自己的Enum作为参数.但我也希望Enum是通用的.
所以我这样声明:
public abstract <T extends Enum<T>> void test(Enum<T> command);
Run Code Online (Sandbox Code Playgroud)
在实现中,我已经知道了那个:
public enum PerspectiveCommands {
PERSPECTIVE
}
Run Code Online (Sandbox Code Playgroud)
并且方法声明变为:
@Override
public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) {
}
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
@Override
public <PerspectiveCommands extends Enum<PerspectiveCommands>> void test(Enum<PerspectiveCommands> command) {
if(command == PerspectiveCommands.PERSPECTIVE){
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
我无权访问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>)
Run Code Online (Sandbox Code Playgroud)
我做了一个这样的解决方法:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有可能不通过我的解决方法?
Tho*_*mas 28
在您的方法中,实现PerspectiveCommands不是枚举,而是您的类型参数,通常会调用它T.因此它像axtavt已经说过的那样影响了同名的枚举,因此PERSPECTIVE在这里是未知的.
您的抽象方法声明很好,但您可能会使用稍微不同的方法.
public void test(PerspectiveCommands command)不起作用,因为这种方法不会覆盖通用版本.原因是使用泛型版本从参数推断出类型,因此您可以传递任何枚举.
但是,我假设您有一个定义抽象方法的接口或抽象类.所以尝试这样的事情:
interface TestInterface<T extends Enum<T>>
{
public abstract void test(T command);
}
class TestImpl implements TestInterface<PerspectiveCommands>
{
@Override
public void test(PerspectiveCommands command) {
if(command == PerspectiveCommands.PERSPECTIVE){
//do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
@迈克的答案是要走的路.
public interface Command1 {
}
public enum MyCommand1 implements Command1 {
}
abstract <E extends Enum<E> & Command1> void execute(E command);
Run Code Online (Sandbox Code Playgroud)
这是另一个版本
// intending to be used only on enums
public interface Command2<E extends Enum<E>> extends Command1 {
}
public enum MyCommand2 implements Command2<MyCommand2> {
}
abstract <E extends Enum<E> & Command2<E>> execute(E command);
Run Code Online (Sandbox Code Playgroud)
正如@axtavt已经指出的那样,问题在于阴影.
如果希望代码按原样运行,可以更改类型变量的名称以删除阴影.
public <C extends Enum<C>> void test(Enum<C> command)
Run Code Online (Sandbox Code Playgroud)
我还要为类型边界添加一个接口,只允许命令枚举,而不是每个枚举派生类的实例.
public <C extends Enum<C> & CommandInterface> void test(Enum<C> command)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79704 次 |
| 最近记录: |