Use of Enum enumeration)

Source: Internet
Author: User

In programs, we often define constants to identify some States and types.

For example, to define the order status, you can define order_status_cancel = 1 to indicate that the order status is "the order has been canceled ".

However, I feel that the constant definition method is not flexible,

For example, if the constant order_status_cancel indicates "the order has been canceled", then:

  1. When saving order data, you can directly use the order_status_cancel constant
  2. When retrieving order data, the constant value 1 is taken out, and the status cannot be determined. You need to manually determine what 1 represents.
  3. Conclusion: It is difficult to expand and maintain in the future.

Given the above considerations, the enumeration class can easily solve the above problems.

  1. You can use order_status_cancel to obtain 1
  2. You can get order_status_cancel through 1.

The Code is as follows:

Public Enum orderstatus {

// 1 order submitted 2 payment pending receipt 3 consumption revocation 4 return
Order_status_submit (1, "coming soon "),
Order_status_paid (2, "paid "),
Order_status_cancel (3, "Consumption revocation "),
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];
}
}
Return NULL;
}
}
Copy code

Instructions for use: orders in the "to be paid" status can be:

OrderStatus.ORDER_STATUS_SUBMIT.getValue()

In this case, the value corresponding to order_status_submit is 1.

 

When you query an order, you need to display the order status (for example, the status is 1, and the corresponding value is "to be paid:

OrderStatus.get(userOrder.getUserOrderStatus()).getText()

 

I personally feel that the enumeration class is quite flexible. For example, if the demand changes suddenly and an order status needs to be added, you can directly add one item to the enumeration class without changing anything else.

Write it out and share it with you. You can view it later and listen to your opinions for better performance.

In programs, we often define constants to identify some States and types.

For example, to define the order status, you can define order_status_cancel = 1 to indicate that the order status is "the order has been canceled ".

However, I feel that the constant definition method is not flexible,

For example, if the constant order_status_cancel indicates "the order has been canceled", then:

  1. When saving order data, you can directly use the order_status_cancel constant
  2. When retrieving order data, the constant value 1 is taken out, and the status cannot be determined. You need to manually determine what 1 represents.
  3. Conclusion: It is difficult to expand and maintain in the future.

Given the above considerations, the enumeration class can easily solve the above problems.

  1. You can use order_status_cancel to obtain 1
  2. You can get order_status_cancel through 1.

The Code is as follows:

Public Enum orderstatus {

// 1 order submitted 2 payment pending receipt 3 consumption revocation 4 return
Order_status_submit (1, "coming soon "),
Order_status_paid (2, "paid "),
Order_status_cancel (3, "Consumption revocation "),
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];
}
}
Return NULL;
}
}
Copy code

Instructions for use: orders in the "to be paid" status can be:

OrderStatus.ORDER_STATUS_SUBMIT.getValue()

In this case, the value corresponding to order_status_submit is 1.

 

When you query an order, you need to display the order status (for example, the status is 1, and the corresponding value is "to be paid:

OrderStatus.get(userOrder.getUserOrderStatus()).getText()

 

I personally feel that the enumeration class is quite flexible. For example, if the demand changes suddenly and an order status needs to be added, you can directly add one item to the enumeration class without changing anything else.

Write it out and share it with you. You can view it later and listen to your opinions for better performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.