1. The difference between enumeration and static constants
When it comes to enumeration, we first think about how it is different from the constants modified by public static final String.
I'll cite two advantages of enumeration:
1. Ensure type safety: the caller cannot pass an int or String value at will;
2. The code is very readable;
For example:
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, the four data elements of spring, summer, autumn and winter form the "data set" of the four seasons.
You have written the method get(String season), and the type you input can only be String type, and the String can only be (spring, summer, autumn, winter).
This time. You write four string constants
public class Common { public static final String SPRING="Spring"; public static final String SEASON="Summer"; public static final String SUMMER="Autumn"; public static final String AUTUMN="Winter";}Putting get(Common.SEASON) in the get method is indeed putting "spring" in it, but at this time you will find that there is a hidden danger here. You get(String season), after all, the String type is put. If a new colleague or an uninformed colleague doesn't know that you can only put "spring, summer, autumn, and winter" in this method. It puts a string such as get("Xiaoxiao"). At this time, it will not report an error during the compilation period. Only after running, it will find that it is wrong.
In order to prevent the above hidden dangers, the enumeration appears
public enum Season { SPRING("Spring"), SUMMER("Summer"), AUTUMN("Autumn"), WINTER("Winter"); ....}At this time, we modify the parameter transfer of the get method and change it to get(Season season). At this time, we add get(Season.SPRING), which can ensure that only a few parameters passed in are.
2. Understand the enumeration
First of all, we need to be clear. In fact, enumeration is also a class class. I will write an enum to understand.
//We treat enums as a common class public enum Season { SPRING(1,"spring"), SUMMER(2,"summer"), AUTUMN(3,"Autumn"), WINTER(4,"winter"); //The last one here must be a semicolon, otherwise an error will be reported/*We can understand it as *public static final Season SPRING = new Season(1, spring); *public static final Season SUMMER = new Season(2, summer); *public static final Season AUTUMN = new Season(3, autumn); *public static final Season WINTER = new Season(4, winter); *Since it is an object, it is easy to understand below*/ /* * 1. There are two parameters in the object above, so there must be a constructor of this type below* 2. This is private, because it cannot be new object*/ private Season(int code,String name) { this.name = name; this.code = code; } //Attributes of the object private String name; private int code; //Methods to obtain object properties public String getName() { return this.name; } public String getCode() { return this.name; } //Get the object through code, we can obtain other properties of the object public static Season decode(int code) { Season season = null; for (Season type : Season.values()) { if (type.code==code) { season = type; break; } } return season; } //Re-toString method public String toString() { return this.name; }}The above example explains the enumeration very well. It is no different from ordinary classes. It just uses another way to create several objects with attributes. This also requires such a constructor with attributes, and that's all.
Here are some of the features of the enumeration:
1. It cannot have a public constructor, and doing so can ensure that the client code cannot create a new instance of enum.
2. Enumeration cannot inherit other classes because it inherits java.lang.Enum by default.
3. The constant value address is unique, and you can use == to directly compare it, and the performance will be improved.
4. Enum also provides the values method, which allows you to easily traverse all enum values.
5. Enum also has an oridinal method, which returns the order of enum values in the enum class, which depends on the order of enum value declaration.
3. Common usage of enumeration
The first type: switch application
Create an enumeration first:
public enum Common { INSERT, MODIFY, DELETE}//Because this is an object without parameters, the system's default constructor can be used. There is no need to write attributes and methods.Writing implementation code
public class CommonUtils { public static void getType(Common common){ Common c=common; switch(c) { case INSERT: System.out.println("Insert operation"); break; case MODIFY: System.out.println("Configure operation"); break; case DELETE: System.out.println("Delete operation"); break; } } public static void main(String[] args) { getType(Common.DELETE); //Background output: Delete operation}}The second usage is to obtain the value value through the key value to obtain other values
Enumeration class
public enum Season { SPRING(1,"spring","spring kite flying in spring"), SUMMER(2,"summer","swimming in summer"), AUTUMN(3,"Autumn","Autumn outing in autumn"), WINTER(4,"winter","winter"season(int code,String name,String bz) { this.code = code; this.name = name; this.bz=bz; } private int code; private String name; private String bz; public static Season decode(int code) { Season season = null; for (Season type : Season.values()) { if (type.code==code) { season = type; break; } } return season; } public int getCode() { return code; } public String getName() { return name; } public String getBz() { return bz; }}Test class
Okay, I'll write so much, and I'll get to know more in-depth if I need it in the future. Thank you for your support to Wulin.com.