枚举常用的使用方式

你。 提交于 2019-11-29 09:56:53

枚举常用的使用方式:

public class TestEmen {
    enum TestInfo{
        ZHUANLI(1,"zhuanli"),
        LUNWEN(3,"lunwen"),
        CHENGJI(3,"chengji");
        private int type;
        private String name;

        TestInfo(int type,String name){
            this.type = type;
            this.name = name;
        }

        /**
         * 当拿到一个枚举成员之后通过调用其get方法获取该成员的type值
         * @return
         */
        public int getType() {
            return type;
        }

        /**
         * 当拿到一个枚举成员之后通过调用该方法获取成员的name值
         * @return
         */
        public String getName() {
            return name;
        }

        /**
         * 根据输入的type类型通过switch的方式获取所对应的枚举类型
         * @param type
         * @return
         */
        public static TestInfo getTestInf(int type){
            switch (type){
                case 1:
                    return ZHUANLI;
                case 2:
                    return LUNWEN;
                case 3:
                    return CHENGJI;
                default:
                    return null;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(TestInfo.getTestInf(1).getType());
        System.out.println(TestInfo.CHENGJI.getName());
    }
}


转载于:https://my.oschina.net/u/580135/blog/612174

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!