JAVA ENUM 枚举的简单使用

介绍

枚举可以用来表示一系列状态,比如颜色、星期、订单状态等,虽然功能很小,但大大方便了开发。

    //定义颜色枚举类
    public enum Color {  
      RED, GREEN, BLANK, YELLOW  
    } 

    Color.RED 
    Color.GREEN

应用

下面展示一下用枚举表示订单状态

    public enum OrderStatus {

        ORDER_STATUS_WAIT_PAY("待支付", 0),
        ORDER_STATUS_WAIT_SEND("待发货", 1),
        ORDER_STATUS_WAIT_RECEIVE("待收货", 2),
        ORDER_STATUS_WAIT_REMARK("待评论", 3),
        ORDER_STATUS_FINISH("已完成", 4),
        ORDER_STATUS_RETURNING("退货中", 5),
        ORDER_STATUS_RETURNED("已退款", 6),
        ORDER_STATUS_EXCHANGEING("换货中", 7),
        ORDER_STATUS_APPLY_RETURN("申请退货", 8),
        ORDER_STATUS_APPLY_EXCHANGE("申请换货", 9),
        ORDER_STATUS_CANCEL("已取消", 10),
        ORDER_STATUS_PAIED("已支付", 11);

        String name;
        int value;

        OrderStatus(String name, int value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public static OrderStatus getType(int value) {
            OrderStatus orderStatus = null;
            for (OrderStatus tmpOrderStatus : values()) {
                if (tmpOrderStatus.getValue() == value) {
                    orderStatus = tmpOrderStatus;
                    break;
                }
            }
            return orderStatus;
        }
    }