美文网首页
java switch语句中使用枚举类

java switch语句中使用枚举类

作者: 金卡戴珊ugly | 来源:发表于2018-12-20 17:09 被阅读0次

    在java中使用枚举类,并使用switch语句

    public class EnumTest {
        public static void main(String[] args) {
            System.out.println("compony give me a phone,what brand is?");
            String phoneBrand = "08";
            switch (CellphoneBrandEnum.getByValue(phoneBrand)) {
            case Apple:
                System.out.println("I hate Apple");
                break;
            case Samsung:
                System.out.println("I hate Samsung");
                break;
            case Huawei:
                System.out.println("I hate Huawei");
                break;
            case Xiaomi:
                System.out.println("I hate Xiaomi");
                break;
            case Oppo:
                System.out.println("I hate Oppo");
                break;
            case Vivo:
                System.out.println("I hate Vivo");
                break;
            default:
                System.out.println("I hate you");
                break;
            }
        }
    }
    
    public enum CellphoneBrandEnum {
    
        Apple("01"), Samsung("02"), Huawei("03"), Xiaomi("04"), Oppo("05"), Vivo("06"), Unknow("");
    
        String brandName;
    
        CellphoneBrandEnum(String name) {
            brandName = name;
        }
    
        String getValue() {
            return brandName;
        }
    
        public static CellphoneBrandEnum getByValue(String value) {
    
            for (CellphoneBrandEnum e : values()) {
    
                if (e.getValue().equals(value)) {
    
                    return e;
    
                }
            }
    
            return CellphoneBrandEnum.Unknow;
        }
    }
    

    如果有不对的,请各位多多指正,谢谢。

    相关文章

      网友评论

          本文标题:java switch语句中使用枚举类

          本文链接:https://www.haomeiwen.com/subject/vgxukqtx.html