Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java enumerates the usage of enum

Shulou Source: shulou.com Published: 2022-06-02 09:57:58 11月25日 Update

This article mainly explains the use of "java enumeration enum". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the usage of java enumeration enum.

1. Construction method

Proprietary construction method

two。 Inheritance relationship

Inherit Enum by default

3. Main methods

Name: the name of the enumerated object

Ordinal: enumerates the order of objects in the class (index)

Values: gets all enumerated objects (returns an array of values)

CompareTo: object comparison

ValueOf: getting enumerated objects

4. Two ways of writing

a. Attribute-free method

Public enum TestEnum {MONDAY,TUESDAY,WEDNESDAY,THURSDAY, FRIDAY,SATURDAY,SUNDAY}

b. Method with attributes

Public enum TestEnum {/ / if you have your own properties, you need to give a construct with parameters, and you need to add corresponding values to the enumeration properties, that is, name MONDAY ("Monday"), TUESDAY ("Tuesday"), WEDNESDAY ("Wednesday"), THURSDAY ("Thursday"), FRIDAY ("Friday"), SATURDAY ("Saturday"), SUNDAY ("Sunday"); / / the semicolon must be added with private String name TestEnum (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name;}}

5. Application

Public static void method () {TestEnum [] testEnums = TestEnum.values (); for (TestEnum t: testEnums) {String name = t.getName (); switch (name) {case "Monday": System.out.println (Monday); break Case "Tuesday": System.out.println ("Tuesday"); break; case "Wednesday": System.out.println ("Wednesday"); break Case "Thursday": System.out.println ("Thursday"); break; case "Friday": System.out.println ("Friday"); break Case "Saturday": System.out.println ("Saturday"); break; case "Sunday": System.out.println ("Sunday"); break Thank you for your reading, the above is the content of "java enumerating the usage of enum". After the study of this article, I believe you have a deeper understanding of the use of java enumerating enum, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Week objects methods properties learning content that is semicolons names ideas situations arrays articles methods more knowledge knowledge points articles indexes follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi NVidia Microsoft MariaDB