Java 枚举,用于开关盒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2836286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:26:23  来源:igfitidea点击:

Enums,use in switch case

javaenums

提问by Apps

I have an Enum defined which contains method return type like "String",Float,List,Double etc.

我定义了一个 Enum,它包含方法返回类型,如“String”、Float、List、Double 等。

I will be using it in switch case statements. For example my enum is

我将在 switch case 语句中使用它。例如我的枚举是

public enum MethodType {
    DOUBLE,LIST,STRING,ARRAYLIST,FLOAT,LONG;
}

In a property file, I've key value pairs as follows. Test1=String Test2=Double

在属性文件中,我有如下键值对。测试 1=字符串测试 2=双

In my code I'm getting the value for the key. I need to use the VALUE in Switch Case to Determine the Type and based on that I've to implement some logic. For example something like this

在我的代码中,我获得了密钥的值。我需要使用 Switch Case 中的 VALUE 来确定类型,并在此基础上实现一些逻辑。例如这样的事情

switch(MethodType.DOUBLE){
     case DOUBLE:
        //Dobule logic
}

Can someone please help me to implement this?

有人可以帮我实现这个吗?

采纳答案by tangens

I guess this is what you are looking for:

我想这就是你要找的:

public class C_EnumTest {
    public enum MethodType {
        DOUBLE,LIST,STRING,ARRAYLIST,FLOAT,LONG;
    }
    public static void main( String[] args ) {
        String value = "DOUBLE";
        switch( MethodType.valueOf( value ) ) {
        case DOUBLE:
            System.out.println( "It's a double" );
            break;
        case LIST:
            System.out.println( "It's a list" );
            break;
        }
    }
}

For not being case sensitive you could do a MethodType.valueOf( value.toUpperCase() ).

如果不区分大小写,您可以执行MethodType.valueOf( value.toUpperCase() ).

回答by JYelton

You've defined the enum, but you need to define a variable that isthat type. Like this:

你定义的枚举,但你需要定义一个变量就是这种类型。像这样:

public enum MethodType { ... }

public MethodType myMethod;

switch (myMethod)
{
    case MethodType.DOUBLE:
        //...
        break;
    case MethodType.LIST:
        //...
        break;
//...
}

Edit:

编辑:

Previously, this snippet used varas the variable name, but that's a reserved keyword. Changed to myMethod.

以前,此代码段用作var变量名,但这是一个保留关键字。改为myMethod.

回答by StevenWilkins

You don't need a switch at all (this is in java btw so might not work for you): Obviously you'll want to add some null checks, better exception handling, etc.

您根本不需要开关(这是在 Java 中,顺便说一句,因此可能不适合您):显然您需要添加一些空检查、更好的异常处理等。

public enum MethodType {

        String,LONG,DOUBLE,THING;

        static MethodType fromString(String x) throws Exception {
            for (MethodType currentType: MethodType.values()){
                if (x.equals(currentType.toString())){
                    return currentType;
                }
            }
            throw new Exception("Unmatched Type");
        }
    }

回答by digitalsanctum

This may be a little closer to what you need. You can make the propertyName property anything you need it to be in this case:

这可能更接近您的需要。在这种情况下,您可以根据需要设置 propertyName 属性:

public enum MethodType {

  STRING("String"),
  LONG("Long"),
  DOUBLE("Double"),
  THING("Thing");

  private String propertyName;

  MethodType(String propName) {
    this.propertyName = propName;
  }

  public String getPropertyName() {
    return propertyName;
  }

  static MethodType fromPropertyName(String x) throws Exception {
    for (MethodType currentType : MethodType.values()) {
      if (x.equals(currentType.getPropertyName())) {
        return currentType;
      }
    }
    throw new Exception("Unmatched Type: " + x);
  }
}