- 枚举类中枚举值序号(类似于数组下标)默认从0开始。要获取它的下标可以调用ordinal()方法实现。
public enum Days{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday;
}
public static void main(String[] args) {
System.out.println(Days.Sunday.ordinal());
}
2. 可以给枚举值添加各种属性,并且可以通过构造函数给枚举值赋予初始值,通过get,set方法更新枚举值的属性值。
public enum OrderStatusEnum {
CANCELED(0, "已取消"),
NO_PAY(10,"未支付"),
PAID(20,"已付款"),
SHIPPED(40,"已发货"),
ORDER_SUCCESS(50,"订单完成"),
ORDER_CLOSE(60,"订单关闭");
private int code;
private String value;
OrderStatusEnum(int code, String value) {
this.code = code;
this.value = value;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static void main(String[] args) {
OrderStatusEnum.CANCELED.setCode(1);
System.out.println(OrderStatusEnum.CANCELED.getCode());
}
3. 枚举类一般可以定义在常量类中,如Const类当中。