• java 枚举


    1.枚举无变量参数

    public enum SeasonEnum{
        SPRING,SUMMER,AUTUMN,WINTER;
    }
    
    public void test()
    {
        SeasonEnum se = SeasonEnum.SPRING;
        switch(se) {
        case SPRING:
            System.out.println("spring");
            break;
        case SUMMER:
            System.out.println("summer");
            break;
        case AUTUMN:
            System.out.println("autumn");
            break;
        case WINTER:
            System.out.println("winter");
            break;
        }
    }    

    2.只传一个值

    public enum SeasonEnum{
        SPRING("1"),SUMMER("2"),AUTUMN("3"),WINTER("4");
        
        private String val;
        SeasonEnum (String val){
            this.val = val;
        }
        
        public String getVal()
        {
            return val;
        }
    }
    
    public void test()
    {
        SeasonEnum se = SeasonEnum.SUMMER;
        System.out.println(se.getVal()); // 2
    }

    3.传入键值

    public enum SeasonEnum{
        SPRING(0,"spring"),SUMMER(1,"summer"),AUTUMN(2,"autumn"),WINTER(3,"winter");
        private int key;
        private String val;
        
        public int getKey() {
            return key;
        }
        
        public String getVal() {
            return val;
        }
        
        SeasonEnum (int key, String val){
            this.key = key;
            this.val = val;
        }
    }
    
    public void test()
    {
        SeasonEnum se = SeasonEnum.WINTER;
        System.out.println(se.getKey()+" -> " + se.getVal()); //  3 -> winter
    }
  • 相关阅读:
    单例模式 MonoState
    适配器模式+AutoFac
    复合模式 泛型检查
    复合模式
    OpenDaylight实验
    Open vSwitch
    Mininet入门实战
    Python基础语句学习
    修改用户密码登录期限
    springboot maven自定义jar包包名, 增加环境、时间版本号信息
  • 原文地址:https://www.cnblogs.com/lin3615/p/4350910.html
Copyright © 2020-2023  润新知