枚举,用于开关盒 [英] Enums,use in switch case

查看:144
本文介绍了枚举,用于开关盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

在属性文件中,我有如下的键值对。
Test1 = String
Test2 = Double

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

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

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(MethodType.DOUBLE){
     case DOUBLE:
        //Dobule logic
}

有人可以帮我实现吗?

推荐答案

我想这就是你要找的东西:

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

因为不区分大小写你可以做 MethodType.valueOf(value.toUpperCase())

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

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

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