简体   繁体   English

用于switch语句的Java扩展枚举

[英]Java extended enum for switch statement

I have the following code 我有以下代码

public interface EnumInterface{
   public String getTitle();
}


public enum Enum1 extends EnumInterface{
  private String title;
  Enum1(String title){
    this.title = title;
  }

  A("Apple"),B("Ball");
  @Override
  public String getTitle(){
    return title;
  }
}

public enum Enum2 extends EnumInterface{
  private String title;
  Enum1(String title){
    this.title = title;
  }

  C("Cat"),D("Doll");
  @Override
  public String getTitle(){
    return title;
  }
}

and I am using it in other class as follows 我在其他课程中使用它如下

private EnumInterface[] enumList;//declared globally.

if(flagTrue){
  enumList = Enum1.values();
}else{
  enumList = Enum2.values();
}
....
....
private method1(int position){
  switch(enumList[postion]){
    case A:....
           break;
    case B:....
           break;
    case C:....
           break;
    case D:....
           break;
  }
}

I am getting the following compilation time error 我收到以下编译时错误

Cannot switch on a value of type EnumInterface. 无法打开EnumInterface类型的值。 Only convertible int values or enum variables are permitted. 只允许使用可转换的int值或枚举变量。

I did my research and found that if I do it this way, the 'switch' case is not possible. 我做了我的研究,发现如果我这样做,'开关'的情况是不可能的。

The Switch Statement is absolutely not what you want in this case. 在这种情况下,Switch语句绝对不是您想要的。 Even if the above example worked, and it does not for a reason, your results would be completely wrong. 即使上面的例子有效,并且没有理由,你的结果也会完全错误。 The switch on an enum is using the enum ordinal, which is 0 for Enum1.A , 1 for Enum1.B , 0 for Enum2.C , and 1 for Enum2.D using your classes above. 上枚举交换机使用枚举序,这是0 Enum1.A ,1 Enum1.B ,0为Enum2.C为,和1 Enum2.D使用上述你的类。 You can see why this would be a really bad idea. 你可以看出为什么这会是一个非常糟糕的主意。

Your EnumInterface class is not tied of the Java type enum in any way, it just defines any class that implements it and provides your getTitle() method. 您的EnumInterface类不以任何方式绑定Java类型enum ,它只定义实现它的任何类并提供getTitle()方法。 This could be Enum1 , Enum2 , or any other class that might not even be an Enum. 这可能是Enum1Enum2或任何其他甚至可能不是Enum的类。 So when you want to switch based on the EnumInterface , you need to ask yourself what you actually want to switch on. 因此,当您想要基于EnumInterface进行切换时,您需要问问自己实际想要打开的内容。 Is it the title you want to be using as your conditional, or do the enums you defined bring something else to the table? 它是你想要用作条件的标题,还是你定义的枚举带来了其他东西?

Now, I'm going to give you the benefit of the doubt and assume that whatever you are trying to do, it needs to be keyed off of an Enum. 现在,我将给你怀疑的好处,并假设无论你想做什么,它都需要从一个Enum键入。 I will also assume that you cannot combine Enum1 or Enum2 for whatever reason. 我还假设您无论出于何种原因都无法组合Enum1Enum2 Below is my completely over-engineered solution for you: 以下是我完全过度设计的解决方案:

public interface EnumInterface {

    public String getTitle();

    public void processEvent(SwitchLogicClass e);
}


public enum Enum1 implements EnumInterface{

    A("Apple"){
        public void processEvent(SwitchLogicClass e){
            //Any A specific Logic
            e.doSomethingA();
        }
    },
    B("Ball"){
        public void processEvent(SwitchLogicClass e){
            //Any B specific Logic
            e.doSomethingB();
        }
    };

    private String title;
    Enum1(String title){
        this.title = title;
    }


    @Override
    public String getTitle(){
        return title;
    }
}

Repeat for Enum2. 重复Enum2。 Assume that the next class is called SwitchLogicClass 假设下一个类叫做SwitchLogicClass

private EnumInterface[] enumList;//declared globally.

if(flagTrue){
    enumList = Enum1.values();
}else{
    enumList = Enum2.values();
}
....
....
private method1(int position){
    EnumInterface[position].processEvent(this);
}


public void doSomethingA(){
    //Whatever you needed to switch on A for
}

public void doSomethingB(){
    //Whatever you needed to switch on B for
}

....
....

You will almost certainly need to refactor based on whatever abstraction patterns you need to use, but the above is the best I can do with what I know of your code. 你几乎肯定需要根据你需要使用的抽象模式进行重构,但上面是我能用你所知道的代码做的最好的。

What did the extending accomplish that can't be done using a simple java enum ? 扩展完成了什么,使用简单的java enum无法完成? You can just use if else blocks and and invoke equals method. 你可以使用if else块并调用equals方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM