[Java Enumeration Class] Use the enum keyword to define an enumeration class

Instructions for use

1. The enumeration class defined by enum inherits the java.lang.Enum class by default, so it cannot inherit other classes

2. The constructor of the enumeration class can only use the private permission modifier

3. All instances of an enumeration class must be explicitly listed in the enumeration class (, delimited; end). The listed instance system will automatically add public static final modification

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

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

=========================================================================

 Use the enum keyword to define an enumeration class

Proceed as follows:

1. Provide multiple objects of the current enumeration class, separate multiple objects with "," and end with ";" at the end
2. Declare the properties of the Season object: private final modification
3. Privateize the constructor of the class and assign values ​​​​to the object properties

Rewrite the custom enumeration class in the previous article below

public class SeasonTest01 {
    public static void main(String[] args) {
        Season1 summer = Season1.SUMMER;
        System.out.println(summer);
    }
}
//使用enum关键词枚举类
enum Season1{
    //1.提供当前枚举类的多个对象,多个对象之间用“,“隔开,末尾对象";"结束
    SPRING ("春天","春暖花开"),
    SUMMER ("夏天","夏日炎炎"),
    AUTUMN ("秋天","秋高气爽"),
    WINTER ("冬天","冰天雪地");


    //2.声明Season对象的属性:private final修饰
    private final String seasonName;
    private final String seasonDesc;

    //3.私有化类的构造器,并给对象属性赋值
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }


    //4.其他诉求1:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    //4.其他诉求1:提供toString()
//    @Override
//    public String toString() {
//        return "Season1{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

As above code, we comment toString(), and the running result is as follows:

We can see that the result of the operation is not the address value. So the parent class of Season1 is not object

Next, let's look at its parent class

System.out.println(Season1.class.getSuperclass());

 Add the above code to the code as follows:

public class SeasonTest01 {
    public static void main(String[] args) {
        Season1 summer = Season1.SUMMER;
        System.out.println(summer);

        System.out.println(Season1.class.getSuperclass());
    }
}
//使用enum关键词枚举类
enum Season1{
    //1.提供当前枚举类的多个对象,多个对象之间用“,“隔开,末尾对象";"结束
    SPRING ("春天","春暖花开"),
    SUMMER ("夏天","夏日炎炎"),
    AUTUMN ("秋天","秋高气爽"),
    WINTER ("冬天","冰天雪地");


    //2.声明Season对象的属性:private final修饰
    private final String seasonName;
    private final String seasonDesc;

    //3.私有化类的构造器,并给对象属性赋值
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }


    //4.其他诉求1:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    //4.其他诉求1:提供toString()
//    @Override
//    public String toString() {
//        return "Season1{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

The result of the operation is as follows:

 Description: The defined enumeration class inherits from the java.lang.Enum class by default

We can override the toString() method

Guess you like

Origin blog.csdn.net/qq_64976935/article/details/129466636