[New Features of Java 5] Enumeration
What is Enumeration type?
If you define a class to complete the employee information of an enterprise, the employee information includes the name, age, and position information (the job has only three roles: WORKER, MANAGER, and BOSS). The complete code is as follows:
Public class Demo {private String name; private Integer age; private String role; public String getRole () {return role;} public void setRole (String role) {this. role = role;} public static void main (String [] args) {Demo emp1 = new Demo (); emp1.setRole (WORKER); Demo emp2 = new Demo (); emp2.setRole (MANAGER); Demo emp3 = new Demo (); emp3.setRole (boss); List
List = new ArrayList
(); List. add (emp1); list. add (emp2); list. add (emp3); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ). getRole ());}}}
In the code above, we can see that when setting a job role for an employee, we can set other roles that are not in the requirement. In this way, our code security is very low (unexpected values may occur), and the code readability is not very good. We can rewrite the above Code as follows:
Public class Demo {private String role; public static void main (String [] args) {Demo emp1 = new Demo (); emp1.role = Role. WORKER; Demo emp2 = new Demo (); emp2.role = Role. MANAGER; Demo emp3 = new Demo (); emp3.role = boss; List
List = new ArrayList
(); List. add (emp1); list. add (emp2); list. add (emp3); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ). role) ;}}} class Role {public static final String BOSS = BOSS; public static final String MANAGER = MANAGER; public static final String WORKER = WORKER ;}
After such rewriting, the code readability is improved. However, other unexpected values can still appear in the job role, which we do not want to see. Therefore, we need to rewrite the above Code:
Public class Demo {private Role role; public static void main (String [] args) {Demo emp1 = new Demo (); emp1.role = Role. BOSS; Demo emp2 = new Demo (); emp2.role = Role. MANAGER; Demo emp3 = new Demo (); emp3.role = boss; // an error is reported here. List
List = new ArrayList
(); List. add (emp1); list. add (emp2); list. add (emp3); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ). role) ;}} class Role {public static final Role BOSS = new Role (); public static final Role MANAGER = new Role (); public static final Role WORKER = new Role (); private Role (){}}
When the above Code is rewritten to this point, the code security and readability issues have been solved. However, Java 5 provides a simpler solution to this problem:
public class Demo { private Role role; public static void main(String[] args) { Demo emp = new Demo(); emp.role = Role.BOSS; }}enum Role { WORKER, MANAGER, BOSS;}
Here, the "enum" keyword indicates the enumeration type, which means that there are three positions in the job: WORKER, MANAGER, and BOSS, and you can only select among the three. The WORKER, MANAGER, and BOSS members in the enumeration type are actually a Role object.
Enumeration and switch
Through the above discussion, we can see that enumeration is suitable for use in switch statements. Let's look at the case code:
Public class Demo {private Role role; public static void main (String [] args) {Demo emp = new Demo (); emp. role = Role. BOSS; switch (emp. role) {case WORKER: System. out. println (this is an employee role .); break; case MANAGER: System. out. println (this is the manager role .); break; case BOSS: System. out. println (this is the boss role .); break ;}} enum Role {WORKER, MANAGER, BOSS ;}
Note thatIn the switch statement, the enumeration class name cannot be used, for example, "case Role. WORKER ". Because the compiler judges each Enumeration type based on emp. role in the switch statement.
Enumerative Constructors
You can use the default constructor or custom constructor in the enumeration class. However, you still need to pay attention to some issues when using the custom constructor.
The code for using the default constructor is as follows:
public enum Role { WORKER, MANAGER, BOSS; private Role() { }}
Note that:
To define the constructor, add the separator ";" to the last enumeration item. The constructor can only use private modifiers.
The code for using the custom constructor is as follows:
public enum Role { WORKER(WORKER), MANAGER(MANAGER), BOSS(BOSS); private Role(String role) { System.out.println(role); }}
If the custom constructor receives parameters, the corresponding information needs to be transmitted in the enumeration items. The specific method is to add "()" after the enumeration item and pass it directly.
Enumeration type members
An enumeration class can contain not only constructors, but also member variables and member methods. For example, the following code:
public enum Role { WORKER, MANAGER, BOSS; private String role; public String getRole() { return role; } public void setRole(String role) { this.role = role; }}
HoweverNote thatWhether it is a constructor or a member, the enumeration item must be defined first in the enumeration class. Other content must be after the enumeration item.
Enumeration classes can also contain abstract methods:
public enum Role { WORKER { @Override public void show() { System.out.println(WORKER); } }, MANAGER { @Override public void show() { System.out.println(MANAGER); } }, BOSS { @Override public void show() { System.out.println(MANAGER); } }; public abstract void show();}
Of course, if an enumeration class contains an abstract method, the enumeration item must implement this abstract method.
Enumeration APIs
In fact, all enumeration classes we use are subclasses of the java. lang. Enum class and inherit all methods of the Enum class. The common method is as follows:
Name (): gets the name of the enumerated item. Ordinal (): gets the index value of the enumeration item. The index value starts from "0. Valueof (Class enumClass, String name): obtains the enumerated Class objects.
Common Methods for customizing enumeration classes are as follows:
Valueof (String name): obtains the enumerated class objects. Values (): although this method cannot be found in the JDK documentation, each enumeration class has this method. It is very convenient to traverse all enumeration values of the enumeration class.