Every sentence of this article is thoughtful. I hope that the old men will definitely gain something when reading it carefully and practicing it.
Abstract: This article mainly discusses the use of enumeration classes in production environment. First, we will introduce the topic we discussed by briefly introducing the concept of enumeration class; then we will go directly to the actual combat part. This article will only introduce the situations that are used more frequently in actual combat and are commonly used, so I hope that Lao Tie can experience and practice it carefully and finally turn it into his own; finally, we will give a brief introduction to the enumeration API. The rest of the content that has not been introduced is basically rarely used in our production environment. If you are interested, you can conduct in-depth research on it yourself.
enumerate
Concept: Enumeration type is part of the new feature in Java 5. It is a special data type. Its characteristic is that our code is more concise and safe. To a certain extent, it allows us to understand business logic more clearly from a global perspective (for example, after the state of an order is defined as an enumeration class, we do not need to look at the business logic code, we only need to understand all the state of our order from this enumeration class, giving us a global impression in our mind, which is more conducive to the later code sorting out.)
Definition: First, use enum to define an enum class; then each enum value (i.e., the declared enum) is separated by commas. If there is operation code after the enum value, then add a semicolon to the last enum value; finally remember that each value declared in the enum class is an instance, that is, there are n enum values, and the constructor is called n times to create n enum instances. Here is a small example:
enum SeasonType { SPRING, SUMMER, AUTUMN, WINTER; SeasonType() { System.out.println("Look at how many times this constructor has been called"); }} public class Season { public static void main(String[] args) { System.out.println(SeasonType.SPRING); }}Console output:
Look at how many times this constructor has been called See how many times this constructor has been called See how many times this constructor has been called See how many times this constructor has been called SPRING
Summary: From here, we can see that each enum value declared in the enum class actually calls the constructor and creates an instance. The simple understanding is: each enum value is an instance object.
No participation in actual combat
(1) Define an enumeration class without parameters
enum SeasonType { SPRING, SUMMER, AUTUMN, WINTER}(2) Use in practice
//Select the following usage according to the actual situation SeasonType springType = SeasonType.SPRING; // Output SPRING String springString = SeasonType.SPRING.toString(); // Output SPRING
There are two practical aspects
(1) Define an enumeration class with only one parameter
enum SeasonType { // Pass parameters through the constructor and create instances SPRING("spring"), SUMMER("summer"), AUTUMN("autumn"), WINTER("winter"); // Define the parameters corresponding to the instance private String msg; // Required: Create an instance for the enum value through this constructor SeasonType(String msg) { this.msg = msg; } // This method can get the parameter value of the corresponding instance public String getMsg() { return msg; }}(2) Use in practice
// When we assign values to an instance class, we can use the following method String msg = SeasonType.SPRING.getMsg(); // Output spring
Three practical measures
(1) Define an enumeration class with two parameters
public enum Season { // Pass parameters through the constructor and create instances SPRING(1, "spring"), SUMMER(2, "summer"), AUTUMN(3, "autumn"), WINTER(4, "winter"); // Define the parameters corresponding to the instance private Integer key; private String msg; // Must write: Create an instance of the enum value through this constructor Season(Integer key, String msg) { this.key = key; this.msg = msg; } // In many cases, the value we may get from the front end is the key of the enum class, and then you can get the corresponding enum value public static Season value ofKey(Integer key) { for (Season season : Season.values()) { if (season.key.equals(key)) { return season; } } throw new IllegalArgumentException("No element matches " + key); } // This method can get the key value of the corresponding instance public Integer getKey() { return key; } // This method can get the msg value of the corresponding instance public String getMsg() { return msg; }}(2) Use in practice
// Export key to 1 Season season = Season.valueofKey(1);// Output keyInteger key corresponding to SPRING instance = Season.SPRING.getKey();// Output msgString corresponding to SPRING instance msg = Season.SPRING.getMsg();
Enumeration class summary
In fact, after the enumeration class understands its concept, the enumeration becomes quite simple. You can write an enumeration class at will. So as for the above practical examples, you must first figure out the concept, and then practice it a few times and it will be OK. I will elaborate on the important concept here to help old people quickly master this knowledge. First of all, remember that the enum values in the enumeration class can have no parameters or multiple parameters, and each enumeration value is an instance; and another important point is that if the enumeration value has n parameters, then there must be n parameter values in the constructor, because each enumeration value declared will call the constructor to create an instance, so the parameters must be one-to-one. Since 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, and then you can get any parameter values in the instance at will through the instance. If you want to make the enumeration class more useful, you can imitate my writing method in Practical 3. Through a certain parameter value, such as the key parameter value, you can get the corresponding enumeration value, and then get whatever value you want.
Enumeration API
The enum classes we define with enum all inherit the java.lang.Enum class, and then they will inherit their API. The commonly used APIs are as follows:
String name()
Get the enum name
int organic()
Get the position of the enum (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 Practical Battle 2 can be used, as long as they are used properly, you can use this method)
values()
Get all enum values in the enumeration class (for example, it was used in actual combat 3)
Summarize
The above is all the content of this article about the actual code sharing of Java programming enumeration, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!