The enum keyword is used to define an enumeration class, which can be implemented as a singleton mode if the enumeration has only one member.
The properties of an enum class object should not be allowed to be altered, so you should use thePrivate FinalModified. Properties that use the private final adornment of an enumeration class should be assigned a value in the constructor. If an enumeration class explicitly defines a constructor with parameters, it must also have a corresponding incoming parameter when listing the enumeration values.the enumeration class object must be declared on the first line of the enumeration class. The difference between an enumeration class and a normal class: 1. Enumeration classes defined with enum inherit the Java.lang.Enum class by default2. The constructor of an enumeration class can only use the private access control3. All instances of an enumeration class must be explicitly listed in the enumeration class (by, delimited, or terminated). The listed instance systems are automatically addedPublic static FinalYou need to customize the enumeration class before modifying JDK1.5:
/*How to customize the enumeration class*/ Public classTestseason { Public Static voidMain (string[] args) {Season Spring=season.spring; SYSTEM.OUT.PRINTLN (Spring); System.out.println (Spring.getseasonname ()); }}//Enum classclassseason{//1. Provide the properties of the class, declared private final Private FinalString Seasonname; Private FinalString Seasondesc; //2. Attribute declared as final, initialized in constructor PrivateSeason (String seasonname,string seasondesc) { This. seasonname=Seasonname; This. seasondesc=Seasondesc; } Public Static FinalSeason spring=NewSeason ("Spring", "Springtime"); Public Static FinalSeason summer=NewSeason ("Summer", "Summer scorching"); Public Static FinalSeason automn=NewSeason ("Automn", "crisp"); Public Static FinalSeason winter=NewSeason ("Winter", "snowy"); //to invoke a property by using a public method PublicString Getseasonname () {returnSeasonname; } PublicString Getseasondesc () {returnSeasondesc; } @Override PublicString toString () {return"Season [seasonname=" + Seasonname + ", seasondesc=" + Seasondesc + "]"; }} The new enum keyword for JDK 1.5 defines an enumeration class: The primary method for enumerating classes: 1.values () Method: Returns an array of objects of the enumerated type. This method makes it easy to iterate through all the enumeration values. 2.valueOf (String str): A string can be converted to the corresponding enumeration class object. The requirement string must be the "first name" of the enumeration class object. If not, there will be a run-time exception.If you need each enumeration value to render a different behavior in the interface method that invokes the implementation, you can have each enumeration value implement the method separately
/** I. Enumeration class * 1. How to customize the enumeration class * 2. How to use the Enum keyword to define an enumeration class * commonly used methods; values (), valueOf (String str) * How to enable an enumeration class to implement an interface: You can make an object invocation of a different enumeration class Overridden abstract methods, performing different effects (equivalent to overriding abstract methods per object) **/ Public classtestseason1{ Public Static voidMain (string[] args) {seasonenum Spring=seasonenum.spring; SYSTEM.OUT.PRINTLN (Spring); System.out.println (Spring.getseasonname ()); //The 1.values method can return an array of objects of an enumerated type, which makes it easy to traverse all enumerated valuesSeasonenum[] Seasons=seasonenum.values (); for(inti=0;i<seasons.length;i++) {System.out.println (seasons[i]); } //The 2.valueOf method can convert a string to a corresponding enumeration class object, requiring the string to be the "first name" of an enumerated class objectString str= "WINTER"; Seasonenum Sea=seasonenum.valueof (str); SYSTEM.OUT.PRINTLN (SEA); //different enumeration class objects call the overridden Show method to get different effectsspring.show (); Sea.show (); }}//InterfaceInterfaceinfo{ Public voidshow ();}//an enumeration class that implements an interfaceenumSeasonenumImplementsinfo{//Enum class object must be declared on the first line of the enumeration classSpring ("Spring", "Spring Blossoms"){ Public voidShow () {System.out.println ("Where is Spring?"); }}, SUMMER ("Summer", "Summer scorching"){ Public voidShow () {System.out.println ("Life is like summer Flowers"); }}, Automn ("Autumn", "crisp"){ Public voidShow () {System.out.println ("The golden Autumn."); }}, WINTER ("Winter", "snowy"){ Public voidShow () {System.out.println ("A fire in the winter."); } }; Private FinalString Seasonname; Private FinalString Seasondesc; Privateseasonenum (String seasonname, String seasondesc) { This. Seasonname =Seasonname; This. Seasondesc =Seasondesc; } PublicString Getseasonname () {returnSeasonname; } PublicString Getseasondesc () {returnSeasondesc; }}
Java Enumeration Classes