版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tiankongzhichenglyf/article/details/53637218
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 {};
}