Java programming enumeration class actual code sharing

This article goes all in to hope that the old fellow will read and fight in real terms, and there will be some harvest.

Absrtact: This paper mainly discusses the use of enumeration classes in production environment. Firstly, we will introduce the concept of enumeration class and introduce the topic we discuss; Then, directly into the actual combat part, this article will only introduce more and more commonly used situations in actual combat, so I hope that the old fellow can experience and practice it and eventually become it. Finally, we will briefly introduce the enumeration API. Other contents that are not introduced are rarely used in our production environment. If you are interested, you can study them in depth.

enumeration

Concept: enumeration type is a new feature in Java 5. It is a special data type. Its feature is to make our code more concise and safe. To some extent, So that we can understand the business logic more clearly from the global perspective (for example, after the status of an order is defined as an enumeration class, we don't need to look at the business logic code. We only need to know all the status of our order from this enumeration class, which gives us an impression of a global view in our mind and is more conducive to sorting out the code in the later stage.)

Definition: first, use enum to define an enumeration class; Then each enumeration value (i.e. declared enumerations) are separated by commas. If there is an operation code after the enumeration value, the last enumeration value should be followed by a semicolon. Finally, we must remember that each value declared in the enumeration class is an instance, that is, if there are n enumeration values, the constructor will be called n times to create n enumeration instances. Here is a small example Look:

Console output:

Summary: it can be seen from here that each enumeration value declared in the enumeration class actually calls the constructor and creates an instance. The simple understanding is that each enumeration value is an instance object.

No participation in actual combat

(1) Define a parameterless enumeration class

(2) Use in actual combat

Actual combat two have one participation

(1) Defines an enumeration class with only one parameter

(2) Use in actual combat

There are two participants in actual combat

(1) Defines an enumeration class with two parameters

(2) Use in actual combat

Enumeration class summary

In fact, after the enumeration class understands its concept, enumeration becomes quite simple. You can write an enumeration class at will. Therefore, the above small practical examples must first understand the concept, and then practice several times. Important concepts, I repeat here, to help the old fellow quickly grasp this knowledge. First, remember that enumerated values in enumerated classes can have no parameters or multiple parameters, and each enumeration value is an instance. Another important point is that if the enumeration value has n parameters, there must be n parameter values in the constructor, because each declared enumeration value will call the constructor to create an instance, so the parameters must correspond one-to-one; Now that we understand this, we only need to define these n parameters as n member variables in the enumeration class, and then provide the corresponding get () method. Then, we can arbitrarily obtain the value of any parameter in the instance through the instance. If you want to make the enumeration class easier to use, you can imitate the way I wrote in practice 3. Through a parameter value, such as the key parameter value, you can get its corresponding enumeration value, and then get what value you want.

Enumeration API

Enum classes defined by enum inherit Java Lang. enum class will inherit its API. The common APIs are as follows:

String name()

Get enumeration name

int ordinal()

Gets the location of the enumeration (subscript, initial value is 0)

valueof(String msg)

Get its corresponding enumeration type through MSG. (for example, the enumeration class or other enumeration classes in actual combat II are OK. This method can be used as long as it is used properly)

values()

Get all the enumeration values in the enumeration class (for example, it was used in the actual combat III)

summary

The above is all about the actual code sharing of Java programming enumeration class in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>