Introduction to the enumeration:
Why use enumeration:
Enumeration is a new type added after Java 1.5. It can be used to define a set of variables with fixed value ranges.
Before the enumeration is released, to define such variables, it is often to define different variables by defining an interface.
Use different integer assignments. But this has obvious disadvantages:
1. The legality of its defined value cannot be guaranteed;
In actual programming, there are often such "data sets", whose values are stable in the program, and the elements in the "data sets" are limited.
For example, seven data elements from Monday to Sunday form a week's "data set", and four data elements from spring, summer, autumn and winter form a "data set" of the four seasons.
How to better use these "datasets" in Java? Therefore, enumeration comes in handy, and the following code introduces the usage of enums in detail.
package com.ljq.test;/*** Detailed explanation of enum usage* * @author jiqinlin* */public class TestEnum {/*** Normal enum* * @author jiqinlin**/public enum ColorEnum {red, green, yellow, blue;}/*** Enumerations can add properties and methods like ordinary classes, and they can add static and non-static properties or methods to it* * @author jiqinlin**/public enum SeasonEnum {//Note: The enum is written in the front, otherwise there will be an error in compilation spring, summer, autumn, winter; private final static String position = "test";public static SeasonEnum getSeason() {if ("test".equals(position))return spring;elsereturn winter;}}/*** Gender* * Implement enum with constructor* * @author jiqinlin**/public enum Gender{//Assign through parentheses, and must have a parameter constructor and a property and method, otherwise there will be an error in compilation//Assignment must be all or non-assigned, and part of the assignment cannot be assigned, and part of it cannot be assigned without assignment; if no assignment, the constructor cannot be written, and the assignment compilation also has an error in MAN("MAN"), WOMEN("WOMEN");private final String value;//The constructor can only be private by default, thus ensuring that the constructor can only use Gender(String value) internally {this.value = value;}public String getValue() {return value;}}/*** Order status* * Implement enum with abstract methods* * @author jiqinlin**/public enum OrderState {/** Cancel*/CANCEL {public String getName(){return "Canceled";}},/** To be reviewed*/WAITCONFIRM {public String getName(){return "To be reviewed";}},/** Waiting for payment*/WAITPAYMENT {public String getName(){return "waiting for payment";}},/** Delivering*/ADMEASUREPRODUCT {public String getName(){return "delivering";}},/** Waiting for delivery*/WAITDELIVER {public String getName(){return "waiting for delivery";}},/** Delivered*/DELIVERED {public String getName(){return "delivered";}},/** Received*/RECEIVED {public String getName(){return "Received";}};public abstract String getName();}public static void main(String[] args) {//Enum is a type used to define variables to limit the assignment of variables; when assigning, the value in the enum is obtained through "enum name.value" ColorEnum colorEnum = ColorEnum.blue;switch (colorEnum) {case red:System.out.println("color is red");break;case green:System.out.println("color is green");break;case yellow:System.out.println("color is yellow");break;case blue:System.out.println("color is blue");break;}//Transip the enum System.out.println("Transip the value in the ColorEnum enum");for(ColorEnum color: ColorEnum.values()){System.out.println(color);}//Get the number of enums System.out.println("The values in the ColorEnum enum are "+ColorEnum.values().length+");//Get the index position of the enum. The default starts from 0 System.out.println(ColorEnum.red.ordinal());//0System.out.println(ColorEnum.green.ordinal());//1System.out.println(Co lorEnum.yellow.ordinal());//2System.out.println(ColorEnum.blue.ordinal());//3// Enumeration implements the java.lang.Comparable interface by default System.out.println(ColorEnum.red.compareTo(ColorEnum.green));//-1//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + SeasonEnum.getSeason());//--------------System.out.println("===========");for(Gender gender : Gender.values()){System.out.println(gender.value);}//--------------System.out.println("===========");for(OrderState order : OrderState.values()){System.out.println(order.getName());}}}