Enumeration classes in java

Use of enumeration classes: 1) Class has only...
Use of enumeration classes:
Enum class:

Use of enumeration classes:

1) Class has only a limited number of objects, which is uncertain. As follows:

                Monday, Sunday

                Gender: Man (male), Woman (female)

                Seasons: Spring, Winter

2) Enumeration classes are strongly recommended when you need to define a set of constants.

3) If there is only one object in the enumeration class, it can be used as an implementation of singleton mode.

4) Implementation of enumeration class: the enumeration class needs to be customized before JDK 1.5. The enum keyword added in JDK 1.5 is used to define the enumeration class

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

6) Enumeration class properties:

        a) The properties of enumeration class objects should not be allowed to be changed, so private final should be used

        b) The private final decorated property of the enumeration class should be assigned a value in the constructor

        c) If the constructor with parameters is defined in the enumeration class, the corresponding incoming parameters must also be used when listing the enumeration values

Custom enumeration class:

public class SeasonTest { public static void main(String[] args){ Season spring=Season.SPRING; System.out.println(spring); } } //Custom enumeration class class Season{ //1. Declare the attribute of Season object: private final modifier private final String seasonName; private final String seasonDesc; //2. Privatize the constructor of the class and assign a value to the object attribute private Season(String seasonName,String seasonDesc){ this.seasonName=seasonName; this.seasonDesc=seasonDesc;; } //3. Provide multiple objects of the current enumeration class public static final Season SPRING=new Season("spring","in the warm spring , flowers are coming out with a rush"); public static final Season SUMMER=new Season("summer","Summer heat"); public static final Season AUTUMN=new Season("autumn","fresh autumn weather"); public static final Season WINTER=new Season("winter","a world of ice and snow"); //4. Other requirements: get the properties of enumeration objects public String getSeasonName(){ return seasonName; } public String getSeasonDesc(){ return seasonDesc; } //4. Other claims 1: toString() @Override public String toString(){ return "Season{"+"seasonName"+seasonName+'\''+ ",seasonDesa='"+seasonDesc+'\''+ '}'; } }

Enum class:

Main methods in Enum class:

Method nameDetailed descriptionvalueOfPassing the Class object and constant name of enumeration type to the static method valueOf will get the enumeration constant matching the parametertoStringGet the name of the current enumeration constant. You can override this method to make the result more readableequalsIn enumeration types, you can directly use "= =" to compare whether two enumeration constants are equal. The equals() method provided by Enum is also implemented directly using "= =". It exists for use in Set, List and Map. Note that equals() is immutable.getDeclaringClassGet the class object of the enumeration type to which the enumeration constant belongs. It can be used to judge whether two enumeration constants belong to the same enumeration type.nameGet the name of the current enumeration constant. It is recommended to use toString() first.ordinalGets the order of the current enumeration constants.compareToThe enumeration type implements the Comparable interface, which allows you to compare the size of both enumeration constants (in the order of declaration)hashCodeEnum implements hashCode() to be consistent with equals(), which is also immutable.cloneEnumeration types cannot be Clone. In order to prevent subclasses from implementing cloning methods, Enum implements an invariant Clone() that only throws CloneNotSupportedException exceptions.

values() method: returns an array of objects of enumeration type. This method can easily traverse all enumerated values.

valueOf(String str): a string can be converted to the corresponding enumeration class object. The string must be the "name" of the enumeration class object. If not, there will be a runtime exception: IllegalArgumentException.

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

/* Define enumeration classes using enum keyword Note: the defined enumeration class inherits from java.lang.Enum by default */ public class SeasonTest1 { public static void main(String[] args){ Season spring=Season.SPRING; System.out.println(spring);//SPRING System.out.println(Season1.class.getSuperclass());//class java.lang.Enum //values(): Season1[] values=Season1.values(); for(int i=0;i<values.length;i++){ System.out.println(values[i]); } Thread.State[] values1=Thread.State.values(); for(int i=0;i<values1.length;i++){ System.out.println(values1[i]);//NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED } //valueOf(String objName): returns the object whose object name is objName in the enumeration class //If there is no enumeration class object of objName, throw an exception: IllegalArgumentException Season1 winter=Season1.valueOf("WINTER"); System.out.println(winter); } } //Define enumeration classes using enum keyword enum Season1 { //1. Provide the object of the current enumeration class. Multiple objects are separated by "," end object "; "End SPRING("spring","in the warm spring , flowers are coming out with a rush"), SUMMER("summer","Summer heat"), AUTUMN("autumn","fresh autumn weather"), WINTER("winter","a world of ice and snow"); //1. Declare the attribute of Season object: private final modifier private final String seasonName; private final String seasonDesc; //2. Privatize the constructor of the class and assign a value to the object attribute private Season1(String seasonName,String seasonDesc){ this.seasonName=seasonName; this.seasonDesc=seasonDesc;; } //4. Other requirements: get the properties of enumeration objects public String getSeasonName(){ return seasonName; } public String getSeasonDesc(){ return seasonDesc; } //4. Other claims 1: toString() @Override public String toString(){ return "Season1{"+"seasonName"+seasonName+'\''+ ",seasonDesa='"+seasonDesc+'\''+ '}'; } }

  Interface of enumeration class defined with enum keyword:

1) Implement the interface and implement the abstract methods in the interface in the enum class

2) Let the objects of the enumeration class implement the abstract methods in the interface respectively

public class SeasonTest { public static void main(String[] args){ Season spring=Season.SPRING; System.out.println(spring); } } //Custom enumeration class class Season{ //1. Declare the attribute of Season object: private final modifier private final String seasonName; private final String seasonDesc; //2. Privatize the constructor of the class and assign a value to the object attribute private Season(String seasonName,String seasonDesc){ this.seasonName=seasonName; this.seasonDesc=seasonDesc;; } //3. Provide multiple objects of the current enumeration class public static final Season SPRING=new Season("spring","in the warm spring , flowers are coming out with a rush"); public static final Season SUMMER=new Season("summer","Summer heat"); public static final Season AUTUMN=new Season("autumn","fresh autumn weather"); public static final Season WINTER=new Season("winter","a world of ice and snow"); //4. Other requirements: get the properties of enumeration objects public String getSeasonName(){ return seasonName; } public String getSeasonDesc(){ return seasonDesc; } //4. Other claims 1: toString() @Override public String toString(){ return "Season{"+"seasonName"+seasonName+'\''+ ",seasonDesa='"+seasonDesc+'\''+ '}'; } }

10 September 2021, 17:17 | Views: 2384

Add new comment

For adding a comment, please log in
or create account

0 comments