Enumeration
1.1 application of enumeration
enum keyword is used to define enumeration class All enumeration classes are subclasses of Enum The first line of an enumeration class must be an enumeration item. The semicolon after the last enumeration item can be omitted. However, if there is something else in the enumeration class, the semicolon cannot be omitted. It is recommended not to omit Enumeration classes can have constructors, but they must be private, which is also private by default. Enumeration items are used in a special way: Enumeration (""); An enumeration class can also have an abstract method, but the enumeration item must override the method Enumeration used in switch statement
Common methods for enumerating classes:
int ordinal() returns the ordinal number of the enumeration item int compareTo(E o) comparing two enumerations returns the difference between the ordinal numbers of two enumerations String name() gets the name of the enumeration item String toString() gets the name of the enumeration item <T> T valueof (class < T > type, string name) is used to obtain the specified enumeration item parameter 1: the bytecode object parameter 2 corresponding to the enumeration class
1.2 characteristics of enumeration
1. All members in enumeration must appear below enumeration object
2. If there is a member in the enumeration class, the semicolon cannot be omitted at the end of the enumeration object
3. The constructor in enumeration must be private
4. The abstract method is meaningful: it is used to describe the information of an enumeration member and improve the readability of the program
5. Enumeration is also one of the constant forms of switch statement
1.3 use of enumeration
Enumeration class:
public enum Direction { FRONT,BEHIND,LEFT,RIGHT; }
Test class call:
public class MyTest1 { public static void main(String[] args) { System.out.println(Direction.FRONT.ordinal());//0 System.out.println(Direction.BEHIND.ordinal());//1 System.out.println(Direction.LEFT.ordinal());//2 System.out.println(Direction.RIGHT.ordinal());//3 Direction left = Direction.LEFT; int index = left.ordinal(); System.out.println(index); String name = left.name(); String s = left.toString(); System.out.println(name);//LEFT System.out.println(s);//LEFT /* //Static method values() gets all enumerations Direction[] values = Direction.values(); for (Direction value : values) { System.out.println(value);*/ /* FRONT BEHIND LEFT RIGHT */ Direction right = Direction.RIGHT; switch (right){ case FRONT: System.out.println("Front"); break; case BEHIND: System.out.println("after"); break; case LEFT: System.out.println("Left"); break; case RIGHT: System.out.println("Execution code"); break; } } }
List enumeration classes:
public enum Direction2 { //FRONT,BEHIND,LEFT,RIGHT; FRONT("Front"), BEHIND("after"), LEFT("Left"), RIGHT("right"),; String name; //Constructor is private private Direction2(String name){ this.name=name; } }
Test class:
public class MyTest2 { public static void main(String[] args) { Direction2 front = Direction2.FRONT; System.out.println(front);//FRONT String name = front.name; System.out.println(name);//Front } }
When there are abstract methods in enumeration, all enumeration items must override methods because enumeration is abstract by default
Enumeration class:
public enum Direction3 { FRONT("Front"){ @Override public void show(String name) { System.out.println(name); } }, BEHIND("after"){ @Override public void show(String name) { System.out.println(name); } }, LEFT("Left"){ @Override public void show(String name) { System.out.println(name); } }, RIGHT("right"){ @Override public void show(String name) { System.out.println(name); } }, ; String name; private Direction3(String name) { //The constructor in enumeration is private decorated this.name = name; } //Provide abstract methods public abstract void show(String name); }
Test class:
public class MyTest3 { public static void main(String[] args) { Direction3 left = Direction3.LEFT; System.out.println(left);//LEFT String name = left.name; System.out.println(name);//Left left.show("Hello");//Hello } }
1.4 singleton mode of enumeration
Singleton mode: only one object of this class is allowed in memory (we can use starved Chinese)
public class Student { //When the class is called, an object is created directly, private static final Student student = new Student(); //Private construct, guaranteed not to be created private Student() { } public static Student getStudent() { return student; } }
Test class:
public class MyTest { public static void main(String[] args) { Student student = Student.getStudent(); Student student1 = Student.getStudent(); System.out.println(student==student1);//true } }
We can also use enumeration classes to write a singleton pattern:
Enumeration class: only one enumeration item
public enum Teacher { TEACHER; }
Test class:
public class MyTest2 { public static void main(String[] args) { Teacher teacher = Teacher.TEACHER; Teacher teacher1 = Teacher.TEACHER; System.out.println(teacher==teacher1);//true } }