Enum

作者: 非非非非常胖的大公鸡9527 | 来源:发表于2018-08-10 10:43 被阅读0次

    java定义新的类型的方式:类、接口、枚举

    public enum Color{
      RED,
      GREEN,
      BLUE;
    }
    

    取出枚举的全部类容:

    for(Color c:Color.values()){     //Color.values()表示得到全部枚举内容
      System.out.println(c);
    }
    
    c.ordinal() //编号
    c.name()  //名字
    

    类集对枚举的支持:EnumMap、EnumSet

    package com.company;
    import java.util.EnumMap;
    import java.util.Map;
    ​
    enum Color{
        RED,GREEN,BLUE;
    }
    ​
    public class Main {
    ​
        public static void main(String[] args) {
            Map<Color,String> demo = new EnumMap<Color, String>(Color.class);
            demo.put(Color.RED,"红");
            demo.put(Color.GREEN,"绿");
            demo.put(Color.BLUE,"蓝");
            System.out.println("/********输出全部内容***********/");
            for(Color c : Color.values()){
                System.out.println(c.name()+":"+demo.get(c));
            }
            System.out.println("/********输出全部key***********/");
            for(Color c : demo.keySet()){
                System.out.println(c);
            }
            System.out.println("/********输出全部values***********/");
            for(String c : demo.values()){
                System.out.println(c);
            }
        }
    }
    

    例子:

    package com.company;
    ​
    enum ResultCode{
        SUCCESS(0),
        UNKNOWN_ERROR(1),
        ILLEGAL_OP_ERROR(2),
        MISS_PARAM_ERROR(3),
        ILLEGAL_PARAM_ERROR(4),
        DUPLICATE_OP_ERROR(20),
        NOT_SUPPORT_OP_ERROR(30),
        TOO_MANY_RECORDS_ERROR(40),
        NO_ORIGIN_VOUCHER(401),
        NO_RETRY_ERROR(500),
        RETRY_LATER(600),
        THRIFT_PROTOCOL_EXCEPTION(1000);
    ​
        private final int value;
    ​
        private ResultCode(int value) {
            this.value = value;
        }
    ​
        /**
         * Get the integer value of this enum value, as defined in the Thrift IDL.
         */
        public int getValue() {
            return value;
        }
    ​
        /**
         * Find a the enum type by its integer value, as defined in the Thrift IDL.
         * @return null if the value is not found.
         */
        public static ResultCode findByValue(int value) {
            switch (value) {
                case 0:
                    return SUCCESS;
                case 1:
                    return UNKNOWN_ERROR;
                case 2:
                    return ILLEGAL_OP_ERROR;
                case 3:
                    return MISS_PARAM_ERROR;
                case 4:
                    return ILLEGAL_PARAM_ERROR;
                case 20:
                    return DUPLICATE_OP_ERROR;
                case 30:
                    return NOT_SUPPORT_OP_ERROR;
                case 40:
                    return TOO_MANY_RECORDS_ERROR;
                case 401:
                    return NO_ORIGIN_VOUCHER;
                case 500:
                    return NO_RETRY_ERROR;
                case 600:
                    return RETRY_LATER;
                case 1000:
                    return THRIFT_PROTOCOL_EXCEPTION;
                default:
                    return null;
            }
        }
    }
    ​
    public class EnumSet {
        public static void main(String[] args){
            System.out.println(ResultCode.SUCCESS);         //输出SUCCESS
            System.out.println(ResultCode.NO_ORIGIN_VOUCHER.getValue());    //输出NO_ORIGIN_VOUCHER的值
            for(ResultCode x:ResultCode.values()){              //遍历枚举内容
                System.out.println(x.getValue());               //输出数值
                System.out.println(x);                          //输出
            }
            Integer i=0;
            System.out.println(i.getClass().getTypeName());
        }
    }
    

    相关文章

      网友评论

          本文标题:Enum

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