Use of enum enum

Source: Internet
Author: User
Tags gettext

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

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.