what is enumeration
The new enum keyword in JDK 1.5 is used to define an enumeration class.
If the enumeration has only one member, it can be used as a singleton mode implementation
enum class definition
Require:
-
The properties of enumeration class objects should not be allowed to be changed, so they should be modified with private final
-
The properties of the enumeration class decorated with private final should be assigned in the constructor
-
If the enumeration class explicitly defines a constructor with parameters, the corresponding parameters must also be passed in when listing the enumeration values
-
The enum class object must be declared on the first line of the enum class.
-
The difference between enumeration class and ordinary class:
- The enumeration class defined by enum inherits the java.lang.Enum class by default
- Constructors of enumeration classes can only use private access control characters
- All instances of the enumeration class must be explicitly listed in the enumeration class (, separated by; end). The listed instance system will automatically add public static final decoration
-
In JDK 1.5, the object of the enumeration class defined by Enum can be used as an expression in the switch expression, and the case clause can directly use the name of the enumeration value without adding the enumeration class as a qualification
-
The properties of enumeration class objects should not be allowed to be changed, so they should be modified with private final
-
The properties of the enumeration class decorated with private final should be assigned in the constructor
-
If the enumeration class explicitly defines a constructor with parameters, the corresponding parameters must also be passed in when listing the enumeration values
-
The enum class object must be declared on the first line of the enum class.
-
The difference between enumeration class and ordinary class:
-
The enumeration class defined by enum inherits the java.lang.Enum class by default
-
Constructors of enumeration classes can only use private access control characters
-
All instances of the enumeration class must be explicitly listed in the enumeration class (, separated by; end). The listed instance system will automatically add public static final decoration
-
In JDK 1.5, the object of the enumeration class defined by Enum can be used as an expression in the switch expression, and the case clause can directly use the name of the enumeration value without adding the enumeration class as a qualification
public enum WeekDays {
ZHOU1("周一"),
ZHOU2("周二");
private final String name;
private WeekDays(String name){
this.name = name;
}
}
Enumeration class-Switch-case statement
WeekDays zhou1 = WeekDays.valueOf("ZHOU1");
switch (zhou1){
case ZHOU1:
System.out.println("123");
break;
case ZHOU2:
System.out.println("456");
break;
}