14 - Java enumeration

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:

  1. The properties of enumeration class objects should not be allowed to be changed, so they should be modified with private final

  2. The properties of the enumeration class decorated with private final should be assigned in the constructor

  3. If the enumeration class explicitly defines a constructor with parameters, the corresponding parameters must also be passed in when listing the enumeration values

  4. The enum class object must be declared on the first line of the enum class.

  5. The difference between enumeration class and ordinary class:

    1. The enumeration class defined by enum inherits the java.lang.Enum class by default
    2. Constructors of enumeration classes can only use private access control characters
    3. 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
  6. 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

  7. The properties of enumeration class objects should not be allowed to be changed, so they should be modified with private final

  8. The properties of the enumeration class decorated with private final should be assigned in the constructor

  9. If the enumeration class explicitly defines a constructor with parameters, the corresponding parameters must also be passed in when listing the enumeration values

  10. The enum class object must be declared on the first line of the enum class.

  11. The difference between enumeration class and ordinary class:

  12. The enumeration class defined by enum inherits the java.lang.Enum class by default

  13. Constructors of enumeration classes can only use private access control characters

  14. 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

  15. 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;
        }

Guess you like

Origin blog.csdn.net/gjb760662328/article/details/129144018