1. 程式人生 > >JAVA 列舉型別使用(結合案例)

JAVA 列舉型別使用(結合案例)

1.定義列舉類

package main.enums;

/**
 * Created by wwm on 2016/7/29.
 */
public enum KjtOrderStatus {
    SYSTEM_CANCEL("-4","系統作廢"),
    CANCEL("-1","作廢"),
    FOR_CHECK("0","待稽核"),
    FOR_OUTBOUND("1","待出庫"),
    HAVE_OUTBOUND_TO_DECLARE("4","已出庫待申報"),
    HAVE_DECLARE_TO_CLEARANCE("41","已申報待通關"),
    HAVE_CLEARANCE_TO_CUSTOMER("45"," 已通關發往顧客"),
    ORDER_FINISHED("5","訂單完成"),
    DECLARE_INVALID_FAILURE_ORDER("6","申報失敗訂單作廢"),
    CLEARANCE_FAILED_ORDER_INVALID("65","通關失敗訂單作廢"),
    ORDER_REJECTED("7","訂單拒收");

    private String code;
    private String desc;

    private KjtOrderStatus(String code, String desc){
        this.code=code;
        this.desc=desc;
    }

    public String getCode() {
        return code;
    }


    public String getDesc() {
        return desc;
    }


    public static KjtOrderStatus valueOfCode(String code){
        for(KjtOrderStatus value:KjtOrderStatus.values()){
            if(value.getCode().equals(code)){
                return value;
            }
        }
        throw new RuntimeException("無匹配的OrderStatus");
    }

}

2.使用列舉類
package main.enums;

/**
 * Created by wwm on 2016/8/3.
 */
public class EnumsTest {
    public static void main(String[] args) {
            KjtOrderStatus orderStatus = KjtOrderStatus.CANCEL;
            switch (orderStatus){
                case CANCEL:
                    System.out.println("測試--"+orderStatus.getDesc());
                    break;
                    default:
                        System.out.println("測試---"+orderStatus.getDesc());
            }
    }
}

3.執行結果


列舉類可以在判斷的時候使程式碼更加優雅,好看