1. 程式人生 > >enum 列舉使用

enum 列舉使用

 

enum 列舉的使用


import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tkzc on 2016/12/14.
 */
public enum QuoteType implements Serializable {

    @SerializedName("0")
    All("預設",0),
    @SerializedName("1")
    TL("整車",1),
    @SerializedName("2")
    LTL("零擔",2);

    private String name;
    private int index;

    private QuoteType(String name, int index) {
        this.name = name;
        this.index = index;

    }

    public  String getDesc(){
        return  this.name.toString();
    }


    public  Short value (){
        return    Short.valueOf(String.valueOf(this.index));
    }

    private static final Map<String, QuoteType> stringToEnum = new HashMap<String,QuoteType>();
    static {
        // Initialize map from constant name to enum constant
        for(QuoteType myenum : values()) {
            stringToEnum.put(myenum.value().toString(), myenum);
        }
    }

    public  static QuoteType valueOf(Short s){
        return  stringToEnum.get(s.toString());
    }

}

values、valueOf方法

Compiled from "Color.java"
public final class Color extends java.lang.Enum<Color> {
  public static final Color Red;
  public static final Color Green;
  public static Color[] values();
  public static Color valueOf(java.lang.String);
  static {};
}