Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the relevant knowledge points of Java enumeration

Shulou Source: shulou.com Published: 2022-06-01 10:50:10 10月02日 Update

This article introduces the relevant knowledge of "what are the relevant knowledge points of Java enumeration?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Define

There are only a limited number of objects in the class.

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

Gender: Man (male), Woman (female)

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

Payment methods: Cash (cash), WeChatPay (Wechat), Alipay (Alipay), BankCard (silver)

Bank Card), CreditCard (Credit Card)

Order status: Nonpayment (unpaid), Paid (paid), Delivered (shipped), Return (return), Checked (confirmed) Fulfilled (shipped),

Thread state: create, ready, run, block, die

Enum

The properties of  enumerated class objects should not be allowed to be changed, so you should use private final decorations

The private final-decorated properties of the  enumerated class should be assigned a value in the constructor

 if the enumeration class explicitly defines a constructor with parameters, then the enumeration value must also correspond to the

Incoming parameter

Class Season {name of private final String SEASONNAME;// season description of private final String SEASONDESC;// season private Season (String seasonName,String seasonDesc) {this.SEASONNAME = seasonName;this.SEASONDESC = seasonDesc;} public static final Season SPRING = new Season ("spring", public static final Season SUMMER = new Season ("summer", public static final Season AUTUMN = new Season ("autumn", public static final Season WINTER = new Season ("winter",} "warm spring flowers"); "hot summer"); "crisp autumn") "Snow"); method

The values () method returns an array of objects of enumerated types. This method can easily traverse all the

Enumerated values.

ValueOf (String str): you can convert a string to the corresponding enumerated class object. Required character

The string must be the name of the enumerated class object. If not, there will be a runtime exception: IllegalArgumentException.

ToString (): returns the name of the current enumeration class object constant

This is the end of the content of "what are the relevant knowledge points of Java enumeration". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Object knowledge season method week knowledge point content parameter name character string attribute more state constructor payment run practical spring blossom snow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Shulou Technology Shulou Information Linux