Java programming enumeration class combat code sharing

  • 2020-11-30 08:23:17
  • OfStack

Every sentence in this article, I hope brother you read and practice hard, 1 will certainly have the harvest.

Abstract: This article focuses on the use of enumeration classes in a production environment. First, we will introduce the topic of our discussion through a brief introduction of the concept of enumerated classes. Then directly into the actual combat part, this paper will only introduce in the actual combat used more, but also more commonly used situation, so I hope brother can experience and practice, finally into their own; A brief introduction to API for enumerations is given at the end. The rest of the content is rarely used in our production environment. If you are interested, you can do further research.

The enumeration

Concept: an enumerated type is Java 5 1 part of the new features, it is a kind of special data type, its characteristic is to make our code much cleaner, security, to some degree, so that we can stand in the perspective of global more clear understanding of the business logic (such as 1 order status is enumerated by we define after class, we don't have to look at the business logic code, only need to know from the enumeration class can be to all of our orders, give us a kind of holistic view impression in your mind, in favor of the carding of the post code.)

Definition: First use enum to define an enumeration class. Each enumeration value (that is, the declared enumeration) is then separated by a comma, and if the enumeration value is followed by operation code, a semicolon is added to the last enumeration value. Last but not least, remember that every value declared in an enumeration class is an instance, that is, with n enumeration values, the constructor is called n times to create n enumeration instances. Here is a small example:


enum SeasonType {
  SPRING, SUMMER, AUTUMN, WINTER;
  SeasonType() {
    System.out.println(" Look at how many times this constructor is called ");
  }
}
public class Season {
  public static void main(String[] args) {
    System.out.println(SeasonType.SPRING);
  }
}

Console output:


 Look at how many times this constructor is called 
 Look at how many times this constructor is called 
 Look at how many times this constructor is called 
 Look at how many times this constructor is called 
SPRING

Summary: As you can see from this, every enumeration value declared in an enumeration class actually calls the constructor and creates an instance. The simple understanding is that every enumeration value is an instance object.

Actual combat 1 has no parameters

(1) Define an unreferenced enumeration class


enum SeasonType {
  SPRING, SUMMER, AUTUMN, WINTER
}

(2) Use in actual combat


//  Choose the following usage according to the actual situation 
SeasonType springType = SeasonType.SPRING;  //  The output  SPRING 
String springString = SeasonType.SPRING.toString();  //  The output  SPRING

Actual combat 2 has 1 parameter

(1) Define an enumeration class with only one argument


enum SeasonType {
  //  Pass the parameters through the constructor and create the instance 
  SPRING("spring"),
  SUMMER("summer"),
  AUTUMN("autumn"),
  WINTER("winter");

  //  Define the parameters corresponding to the instance 
  private String msg;

  //  Required: Create an instance of an enumerated value with this constructor 
  SeasonType(String msg) {
    this.msg = msg;
  }

  //  This method can be used to obtain the parameter value of the corresponding instance 
  public String getMsg() {
    return msg;
  }
}

(2) Use in actual combat


//  When we assign a value to an instance class, we use the following method 
String msg = SeasonType.SPRING.getMsg();  //  The output  spring

Actual combat 3 has two ingredients

(1) Define an enumeration class with two parameters


public enum Season {
  //  Pass the parameters through the constructor and create the instance 
  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;

  //  Required: Create an instance of an enumerated value with this constructor 
  Season(Integer key, String msg) {
    this.key = key;
    this.msg = msg;
  }

  //  In many cases, the values we might get from the front end are enumerated classes  key  , the corresponding enumeration value can then be obtained through the following static method 
  public static Season valueofKey(Integer key) {
    for (Season season : Season.values()) {
      if (season.key.equals(key)) {
        return season;
      }
    }
    throw new IllegalArgumentException("No element matches " + key);
  }

  //  Through this method, the corresponding instance can be obtained  key  value 
  public Integer getKey() {
    return key;
  }

  //  Through this method, the corresponding instance can be obtained  msg  value 
  public String getMsg() {
    return msg;
  }
}

(2) Use in actual combat


//  The output  key  for  1  An instance of an enumerated value 
Season season = Season.valueofKey(1);
//  The output  SPRING  Instance corresponding  key
Integer key = Season.SPRING.getKey();
//  The output  SPRING  Instance corresponding  msg
String msg = Season.SPRING.getMsg();

Enumeration Class Summary

In fact, after the enumeration class understand its concept, enumeration becomes quite simple, you can easily write an enumeration class out. So make sure you understand the concepts first, and then practice ok a few times. The important concept is repeated here to help brother quickly master this knowledge. First of all, remember that the enumeration value in an enumeration class can have no parameters or can have multiple parameters. Each enumeration value is an instance. It is also important to note that if the enumeration value has n arguments, then the constructor must have n arguments. Since each declared enumeration value calls the constructor to create an instance, the argument 1 must correspond to 11. Now that we understand this point, we just need to define the n parameters as n member variables in the enumeration class, then provide the corresponding get() method, and then we can get any parameter values in the instance through the instance. If you want to make the enumeration class more useful, you can copy what I wrote in real World 3. You can get the corresponding enumeration value by using a certain parameter value, such as the key parameter value, and then whatever value you want, get value is fine.

Enumeration API

The enumeration class defined by enum is inherited from java. lang. Enum, so it inherits API.

String name()

Get the enumeration name

int ordinal()

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

valueof(String msg)

Get its corresponding enumerated type through msg. (Such as the enumerated class in Actual Combat 2 or any other enumerated class, you can use this method as long as you use it properly)

values()

Gets all the enumeration values in the enumeration class (for example, used in Real World 3)

conclusion

Above is the Java programming enumeration class on the actual code to share all the content, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: