Java programming enumeration class practice code sharing, java programming enumeration practice

Source: Internet
Author: User

Java programming enumeration class practice code sharing, java programming enumeration practice

In this article, I hope that the old iron will read it carefully and practice it in real time.

Abstract: This article mainly discusses the use of enumeration classes in the production environment. First, we will introduce the topic of our discussion through a brief introduction to the enumeration class concept. Then we will go directly to the actual practice Section. This article will only introduce more things that are used in actual practice, it is also quite common, so I hope that the old iron can understand and practice it, and ultimately turn it into its own. At last, I will give a brief introduction to the enumerated API. The rest are not introduced and are rarely used in our production environment. If you are interested, you can study it in depth.

Enumeration

Concept: Enumeration type is part of the new feature in Java 5. It is a special data type. It is characteristic of making our code more concise and secure. To some extent, this allows us to better understand the business logic from a global perspective (for example, after the status of an order is defined as an enumeration class, we do not need to look at the business logic code, we only need to know all the statuses of this order from this enumeration class. This gives us a kind of overall view in our mind, which is more conducive to code sorting later .)

Definition: first, use enum to define an enumeration class. Then, each enumeration value (that is, the declared enumeration value) is separated by commas (,). If the enumerated value is followed by the operation code, add a semicolon to the end of the last enumerated value. Remember that each value declared in the enumeration class is an instance, that is, there are n enumerated 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 ("check how many times this constructor has been called") ;}} public class Season {public static void main (String [] args) {System. out. println (SeasonType. SPRING );}}

Console output:

Check whether the constructor has been called several times.

Conclusion: It can be seen from this that each enumerated value declared in the enumeration class actually calls the constructor and creates an instance. A simple understanding is that each enumerated value is an instance object.

No arguments in practice

(1) define a non-Parameter Enumeration class

enum SeasonType {  SPRING, SUMMER, AUTUMN, WINTER}

(2) Practical Use

// Select the following method based on the actual situation. SeasonType springType = SeasonType. SPRING; // output SPRING String springString = SeasonType. SPRING. toString (); // output SPRING

Practice 2: One Parameter

(1) Defining enumeration classes with only one parameter

Enum SeasonType {// PASS Parameters through the constructor and create the instance SPRING ("spring"), SUMMER ("summer"), AUTUMN ("autumn "), WINTER ("winter"); // defines the private String msg parameter corresponding to the instance; // required: use this constructor to create an instance SeasonType (String msg) {this. msg = msg;} // you can obtain the parameter value of the corresponding instance. public String getMsg () {return msg ;}}

(2) Practical Use

// When assigning values to an instance class, use the following String msg = SeasonType. SPRING. getMsg (); // output spring

In practice, there are two parameters.

(1) Enumeration classes defining 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"); // defines the parameter private Integer key; private String msg; // required: use this constructor to create an instance Season (Integer key, String msg) {this. key = key; this. msg = msg;} // in many cases, we may obtain the key of the enumeration class from the front end, then, you can obtain the 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 );} // This method can be used to obtain the key value of the corresponding instance public Integer getKey () {return key ;} // This method can be used to obtain the msg value of the corresponding instance public String getMsg () {return msg ;}}

(2) Practical Use

// Output the instance Season season = Season with the enumerated value of key 1. valueofKey (1); // output the keyInteger key = Season corresponding to the SPRING instance. SPRING. getKey (); // output msgString msg = Season for the SPRING instance. SPRING. getMsg ();

Enumeration class Summary

In fact, after an enumeration class understands its concept, enumeration becomes quite simple. You can easily write an enumeration class. Therefore, we must first clarify the concepts in the above examples and then practice them several times. I will repeat the important concepts here to help the old iron master this knowledge quickly. First, remember that the enumerated values in the enumeration class can have no parameters or multiple parameters, each enumerated value is an instance. It is also important that if there are n parameters in the enumerated value, there must be n parameter values in the constructor, since every declared enumerated value calls the constructor to create an instance, the parameters must correspond one to one, then we only need to define the n parameters as n member variables in the enumeration class, and then provide the corresponding get () method, then, you can obtain any parameter value from the instance. If you want to make the enumeration class more useful, you can use a certain parameter value, such as the key parameter value, as I wrote in Practice 3, you can get the corresponding enumerated value and get the desired value.

Enumeration API

The enumeration classes defined by enum inherit from java. lang. Enum classes, so they inherit from their APIs. The common APIs are as follows:

String name ()

Retrieve enumeration name

Int ordinal ()

Obtain the enumerated position (subscript, initial value: 0)

Valueof (String msg)

Use msg to obtain the corresponding Enumeration type. (For example, in practice, the enumeration class or other enumeration classes can be used. This method can be used as long as it is used properly)

Values ()

Obtain all the enumerated values in the enumeration class (for example, it is used in practice)

Summary

The above is all the content shared in this article about the Java programming enumeration code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.