static:class variable, class 未被instance 就被宣告完成且可以使用、所有class 是參考同一個變數
final:使用之後此變數即是常數,無法被繼承也無法被修改
12345678
public Class StateMachines {
public static final String ORDER_PLACED = "1";
public static final String PAID = "2";
public static final String SHIPPING = "3";
public static final String SHIPPED = "4";
public static final String ORDER_CANCELLED = "5";
public static final String GOOD_RETURNED = "6";
}
Enum 使用方法
定義常數,例如要定義購買流程狀態,就定出已下訂、已付款…等代碼
123
public enum StateMachines {
ORDER_PLACED,PAID,SHIPPING,SHIPPED,ORDER_CANCELLED,GOOD_RETURNED;
}
定義常數內容、overriding、switch case
123456789101112131415161718192021222324
public enum StateMachines {
ORDER_PLACED(1),PAID(2),SHIPPING(3),SHIPPED(4),ORDER_CANCELLED(5),GOOD_RETURNED(6);
private int step;
private Direction(int step) {
this.step = step;
}
@Override
public String toString() {
return String.valueOf(this.step);
}
public String getChName() {
switch(this) {
case ORDER_PLACED:
return "已下訂";
case PAID:
return "已付款";
// 略
}
}
}