介紹
列舉可以用來表示一系列狀態,比如顏色、星期、訂單狀態等,雖然功能很小,但大大方便了開發。
//定義顏色列舉類
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;
}
}複製程式碼