Detailed explanation of Java (enum) enumeration usage

concept

Enum, the full name of enum, is a new feature introduced in JDK 1.5.

In Java, the type modified by enum keyword is enumeration type. The form is as follows:

If no method is added to the enumeration, the enumeration value defaults to an ordered value starting from 0. Take the color enumeration type as an example. Its enumeration constants are red: 0, green: 1, blue: 2

Benefits of enumeration: constants can be organized and managed uniformly.

Typical application scenarios of enumeration: error code, state machine, etc.

The nature of enumeration types

Although enum looks like a new data type, in fact, enum is a restricted class and has its own methods.

When creating enum, the compiler will generate a related class for you, which inherits from Java lang.Enum。

java. Lang. enum class declaration

Enumeration method

In enum, some basic methods are provided:

Values (): returns an array of enum instances, and the elements in the array strictly maintain the order in which they were declared in enum.

Name (): returns the instance name.

Ordinal (): returns the order of instance declaration, starting from 0.

Getdeclarangclass(): returns the enum type to which the instance belongs.

Equals (): judge whether it is the same object.

You can use = = to compare enum instances.

In addition, Java Lang. enum implements the comparable and serializable interfaces, so it also provides the CompareTo () method.

Example: show the basic method of enum

output

Enumeration properties

The characteristics of enumeration can be summed up in one sentence:

In addition to not inheriting, enum can basically be regarded as a regular class.

But this sentence needs to be broken down to understand. Let's go through it carefully.

Enumeration can add methods

As mentioned in the concept section, enumeration values are ordered values starting from 0 by default. Then the question arises: how to assign values to the enumeration display.

Java does not allow assignment of enum constants with =

If you have been exposed to C / C + +, you will naturally think of the assignment symbol =. Enum in C / C + + language can assign values to enumeration constants with the assignment symbol = displayed; Unfortunately, the assignment symbol = is not allowed in Java syntax to assign values to enumeration constants.

Example: enumeration declaration in C / C + + language

Enum can add common methods, static methods, abstract methods and construction methods

Although Java cannot directly assign values to instances, it has a better solution: add methods to enum to indirectly implement display assignment.

When you create an enum, you can add a variety of methods to it, and you can even add construction methods to it.

Note one detail: if you want to define a method for enum, you must add a semicolon at the end of the last instance of enum. In addition, in enum, an instance must be defined first, and fields or methods cannot be defined in front of the instance. Otherwise, the compiler will report an error.

Example: comprehensively show how to define common methods, static methods, abstract methods and construction methods in enumeration

Note: the above example is not desirable, just to show that enumeration supports the definition of various methods. Here is a simplified example

Example: definition of an error code enumeration type

The execution results of this example and the above example are exactly the same.

Enumerations can implement interfaces

Enum can implement interfaces like normal classes.

It also implements the error code enumeration class in the previous section. Its methods can be constrained by implementing the interface.

Enumeration cannot inherit

Enum cannot inherit from another class. Of course, it cannot inherit from another enum.

Because enum actually inherits from Java Lang. enum class, while Java does not support multiple inheritance, so enum can no longer inherit other classes, and of course, it can not inherit another enum.

Enumerated application scenarios

Organization constant

At jdk1 Before 5, constants defined in Java were public static final type A; Such a form. With enumeration, you can organize related constants to make the code easier to read and safer, and you can also use the methods provided by enumeration.

Format of enumeration declaration

Note: if there is no method defined in the enumeration, you can also add comma, semicolon or nothing after the last instance.

The following three declarations are equivalent:

Switch state machine

We often use switch statements to write state machines. After JDK7, switch supports parameters of int, char, string and enum types. Compared with these types of parameters, the switch code using enumeration is more readable.

Organization enumeration

Enumerations of similar types can be organized through interfaces or classes.

However, it is generally organized by interface.

The reason is: the java interface will automatically add the public static modifier to the enum type during compilation; The Java class automatically adds the static modifier to the enum type at compile time. See the difference? Yes, that is, organize enum in the class. If you don't modify it to public, you can only access it in this package.

Example: organize enum in interface

Example: organize enum in class

This example has the same effect as the previous example.

strategy enum

An enumeration of policies is shown in effective Java. This enumeration classifies enumeration constants by enumerating nested enumerations.

Although this approach is not as concise as the switch statement, it is more secure and flexible.

Example: Policy enumeration example in effectviewjava

test

Enumset and enummap

Java provides two tool classes that facilitate the operation of enum -- enumset and enummap.

Enumset is a high-performance implementation of enum types. It requires that the enumeration constants put into it must belong to the same enumeration type.

Enummap is a map implementation tailored to enumeration types. Although other map implementations are used (such as HashMap) can also complete the enumeration type instance to value mapping, but using enummap will be more efficient: it can only receive instances of the same enumeration type as key values, and because the number of enumeration type instances is relatively fixed and limited, enummap uses arrays to store the values corresponding to enumeration types. This makes enummap very efficient.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>