Java枚举作为枚举中的通用类型 [英] Java Enum as generic type in Enum

查看:155
本文介绍了Java枚举作为枚举中的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个抽象类中创建一个抽象方法,它将自己的枚举作为参数。但是我也希望这个枚举将是通用的。

I'm trying to create an abstract method in a abstract class that takes my own Enum as argument. But I want also that that Enum will be generic.

所以我宣布这样:

public abstract <T extends Enum<T>> void test(Enum<T> command);

在实现中,我有en enum作为一个:

In the implementation, I have en enum as that one:

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 ,并显示错误: / p>

I don't have access to the PerspectiveCommands.PERSPECTIVE with the error:

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>)

我已经做了一个解决方法这个:

I've made a workaround like this:

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;
    }
}

但是我想知道是否可以通过我的解决方法?

But I would like to know if it's possible to not pass by my workaround?

推荐答案

在您的方法实现中 PerspectiveCommands 不是枚举,但您的类型参数,通常称为 T 。因此,它影响了同名的枚举,如axtavt已经说过,因此 PERSPECTIVE 在这里是未知的。

In your method implementation PerspectiveCommands is not the enum but your type parameter, which often is called T. It thus shadows the enum of the same name like axtavt already said, thus PERSPECTIVE is unknown here.

方法声明很好,但您可能会使用稍微不同的方法。

Your abstract method declaration is fine but you might use a slightly different approach.

public void test(PerspectiveCommands命令)不会工作,因为这种方法不会覆盖通用版本。原因是使用通用版本,该类型是从参数推断的,因此您可以传递任何枚举。

public void test(PerspectiveCommands command) would not work, since this method would not override the generic version. The reason is that with the generic version the type is inferred from the parameter and thus you could pass any enum.

但是,我假设您有一个接口或抽象类,定义抽象方法。所以尝试这样的东西:

However, I assume you have an interface or abstract class which defines the abstract method. So try something like this:

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
    }
  }
}

这篇关于Java枚举作为枚举中的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆