1. Origin of Enumeration
Assume a scenario first, and now there is a requirement.You are asked to create an Employee class with an attribute role.The company has three roles: BOSS, MANAGER and WORKER.So how do we define this class? We'll implement it step by step.
Let's first set the property role to String, then the Employee class is defined as follows:
class Employee{ private String name; private String role; /** Omit some methods**/ public Employee(String role){ this.role = role; } }
At this point, you can create three employees with different roles:
@Test public void demo1(){ Employee boss = new Employee("BOSS"); Employee manager = new Employee("MANAGER"); Employee worker = new Employee("WORKER"); //But if the words are misspelled, the program will fail Employee boss2 = new Employee("BOS"); }
There are obviously a lot of problems, so we can try to use int so that it's not easy to make mistakes:
class Employee{ private int role; }
Assume 1 is BOSS, 2 is MANAGER, and 3 is WORKER.Then you can create it as follows:
@Test public void demo2(){ Employee boss = new Employee(1); Employee manager = new Employee(2); Employee worker = new Employee(3); //But if you assign other values, you will also have problems Employee worker = new Employee(4); }
At this point, there is still a problem assigning other values, and the code is much less readable.So I think of using static variables:
class Rold{ public static int BOSS = 1; public static int MANAGER = 2; public static int WORKER = 3; }
At this point you can assign values using static variables:
@Test public void demo3(){ Employee boss = new Employee(Role.BOSS); Employee manager = new Employee(Role.MANAGER); Employee worker = new Employee(Role.WORKER); //But other numbers can still be assigned at this time Employee boss2 = new Employee(4); }
Readability is better at this point, but there are still security issues.At this point you can change the type of role to another custom object.Let's modify the Role object:
class Role{ public static final Role BOSS = new Role(); public static final Role MANAGER = new Role(); public static final Role WORKER = new Role(); }
It looks like it's okay at this point. Let's assign values and see:
@Test public void demo4(){ Employee boss = new Employee(Role.BOSS); Employee manager = new Employee(Role.MANAGER); Employee worker = new Employee(Role.WORKER); //You can still have a new anonymous Role() Employee boss2 = new Employee(new Role()); }
At this time, there are still unsafe places.Actually, this is a good solution, as long as the constructor is private.So Role is ultimately like this:
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(){ } }
This writes out an enumeration class.JDK itself provides an implementation of enumeration, which is also simple:
enum Role{ BOSS, MANAGER, WORKER; }
This enumeration Role has the same effect as the Role class above, and their writings are equivalent.
2. Enumeration Details
Next, we will talk about enumeration.
1. Create and use: This is clearly stated above. Take it here.
enum Role{ item1, item2, item3,....; }
2. Parametric construction of enumerations
enum Role{ //Incoming parameter Pass in parameter with parentheses after enumerating objects BOSS(1), MANAGER(3), WORKER(5); //Private can be removed, and the construction method in enum defaults to private (not public) private Role(int level){ } }
3. Define abstract methods in enumerations
enum Role{ //Implement abstract methods anonymously in enumerations BOSS{ public void say(){ System.out.println("I am just bored and want to say something"); } }, MANAGER{ public void say(){ System.out.println("I'm also bored and want to say a word"); } }; public abstract void say(){ } }
4. Enumerate common API s
Enumeration classes are basic java.lang.Enum classes, so let's talk about Enum's common methods:
public void demo4(){ Role role = Role.BOSS; //Returns the name of the enumeration BOSS String name = role.name(); //Returns the subscript of an enumeration int index = role.ordinal(); //Converts the corresponding string name to an enumerated object of the enumClass class Role boss = Enum.valueOf(Role.class, "BOSS"); }