In the program, we often define constants to identify some states, types, and so on.
For example, define the status of an order, which can be defined as Order_status_cancel = 1 to indicate that the order status is "Order canceled."
But the way the senses define constants is not very flexible,
For example, Order_status_cancel constant means "order canceled", then: when the order data, you can directly use the Order_status_cancel constant to fetch the order data, take out the constant value of 1, can't judge what state, need artificial judgment 1 represents what Conclusion: This is not easy to extend maintenance later, more rigid
Given the above considerations, think of the enumeration class, can easily solve the above problem can be obtained by Order_status_cancel 1 can be obtained by 1 Order_status_cancel code as follows:
public enum Orderstatus {
//1 Order submission 2 has been paid pending receipt 3 consumption revocation 4 return
order_status_submit (1, "pending payment"),
Order_status_paid (2, "" Paid "),
Order_status_cancel (3," consumption revoked "),
Order_status_refund (4," returned ");
private int value;
private String text;
Orderstatus (int value, String text) {
this.value = value;
This.text = text;
public int GetValue () {return
this.value;
}
Public String GetText () {return
this.text;
}
public static orderstatus get (int value) {
orderstatus[] elements = orderstatus.values ();
for (int i = 0; i < elements.length i++) {
if (elements[i].getvalue () = = value) {return
elements[i];
}
//repaymenttype.get (2). GetText ();
return null;
}
}
Instructions for use: If an order is deposited in the "pending payment" status, it can:
OrderStatus.ORDER_STATUS_SUBMIT.getValue ()
At this point, we get the order_status_submit corresponding value:1.
When you query for an order, you need to display the order status (if the status is 1 and the corresponding value is "to be paid"):
Orderstatus.get (Userorder.getuserorderstatus ()). GetText ()
Personal feeling enum class is also very flexible, such as demand mutation, need to add a new order state, you can directly in the enumeration class to add a new item, the other need not change
Specially written to share, but also easy to see later, but also willing to listen to everyone's opinions, do better