In depth study of the application of Java enumeration

I Differences between enumerations and static constants

When it comes to enumeration, let's first think about the difference between it and the constant modified by public static final string.

Let me cite two advantages of enumeration:

1. Ensure type safety: the caller cannot arbitrarily pass an int or string equivalent;

2. Code readability is very high;

for instance:

In actual programming, there are often such "data sets", their values are stable in the program, and the elements in the "data set" are limited. For example, four data elements of spring, summer, autumn and winter form the "data set" of the four seasons.

You wrote the method get (string season), the input type can only be string type, and the string can only be (spring, summer, autumn and winter).

This time. You write four string constants

Putting get (common. Session) into the get method does put "spring" into it, but at this time, you will find that there is a hidden danger. After all, you get (string season). After all, what you put in is a string type. If a new colleague or an unknown colleague doesn't know, you can only put "spring, summer, autumn and winter" in this method, It puts a string, such as get ("little"). At this time, it will not report an error during the compilation period. Only after running can it find an error.

In order to prevent the above hidden dangers, enumeration appears

At this time, we modify the parameters of the get method to get (season). At this time, we add get (season. Spring), which can ensure that only these parameters can be passed in.

II Understanding enumeration

First of all, let's make it clear that enumeration is also a class class. I'll write an enumeration to understand it.

The above example explains enumeration very well. It is no different from ordinary classes. It just creates several objects with attributes in another way, which must also write such constructors with attributes, that's all.

Here are some features of enumeration:

1. It cannot have a public constructor, which can ensure that the customer code cannot create a new instance of enum. 2. Enumeration cannot inherit other classes because it inherits Java by default lang.Enum 3. Constant value address is unique. You can use = = for direct comparison, and the performance will be improved 4. Enum also provides the values method, which enables you to easily traverse all enumerated values. 5. Enum also has an ordinal method, which returns the order of enumeration values in enumeration classes, which depends on the order of enumeration value declaration.

III Common usage of enumeration

The first: Switch Application

Create an enumeration first:

Writing implementation code

The second usage is to obtain the value value through the key value to obtain other values

Enumeration class

Test class

Well, that's all. I'll have a deeper understanding in the future. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>