[Java enumeration class] custom enumeration class

One: the use of enumeration classes

main content:

 How to customize enum class

 How to define an enumeration class using the keyword enum

 The main method of the Enum class

 Enum class that implements the interface

There are only a limited number of objects of the class, which are determined . Examples are as follows:

1. Week: Monday (Monday), ..., Sunday (Sunday)

2. Gender: Man (male), Woman (female)

3. Season: Spring (Spring Festival)...Winter (Winter)

4. Payment method: Cash (cash), WeChatPay (WeChat), Alipay (Alipay), BankCard (bank card), CreditCard (credit card)

5. Employment status: Busy, Free, Vocation, Dimission

6. Order status: Nonpayment (unpaid), Paid (paid), Delivered (delivered), Return (returned), Checked (confirmed) Fulfilled (allocated),

7. Thread status: created, ready, running, blocked, dead

 When you need to define a set of constants, it is strongly recommended to use enumeration classes

Implementation of enum class

   >JDK1.5 requires a custom enumeration class

   >JDK 1.5's new enum keyword is used to define enumeration classes

If the enumeration has only one object, it can be used as an implementation of the singleton pattern.

Enum class properties

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 modified with private final should be assigned in the constructor

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

incoming parameters

==================================================== ========================== Custom enumeration class

1. The constructor of the privatized class ensures that its objects cannot be created outside the class

2. Create an instance of the enumeration class inside the class. Declared as: public static final

3. If the object has instance variables, it should be declared as private final and initialized in the constructor

for example:

public class SeasonTest {
    public static void main(String[] args) {
        Season spring = Season.SPRING;
        System.out.println(spring);
    }
}
//自定义枚举类
class Season{
    //1.声明Season对象的属性:private final修饰
    private final String seasonName;
    private final String seasonDesc;

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

    //3.提供当前枚举类的多个对象:public static final
    public static final Season SPRING = new Season("春天","春暖花开");
    public static final Season SUMMER = new Season("夏天","夏日炎炎");
    public static final Season AUTUMN = new Season("秋天","秋高气爽");
    public static final Season WINTER = new Season("冬天","冰天雪地");

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

    public String getSeasonDesc() {
        return seasonDesc;
    }

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

The result of the operation is as follows:

thanks for watching! ! ! 

Guess you like

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