Enumeration
Creates a set of limited sets as a new type, and the values inside the collection can be used as program components;
Enumeration basic attributes
The following code is a simple use of enumerations:
Use the values method to return an array of enum instances
Use the Ordinal method to return the order of each enum instance, starting with 0
Comparison of two enum instances using the CompareTo method
Use = = to compare enum instances
Use the Getdeclaringclass method to return the Enum class to which the enum instance belongs
Use the ValueOf method to return an enum instance based on the specified name
Packageenumerated;enumShrubbery {GROUND, crawling, hanging} Public classEnumclass { Public Static voidMain (string[] args) { for(Shrubbery s:shrubbery.values ()) {System.out.println (s+ "Ordinal:" +s.ordinal ()); System.out.println (S.compareto (shrubbery.crawling)+ " "); System.out.println (S.equals (shrubbery.crawling)+ " "); System.out.println (S==shrubbery.crawling); System.out.println (S.getdeclaringclass ()); System.out.println (S.name ()); System.out.println ("----------------------"); } //produce an enum value from a string name: for(String s: "Hanging crawling GROUND". Split ("") {shrubbery shrub= Enum.valueof (shrubbery.class, s); System.out.println (shrub); } }}
Note Enum enum is inherited from the Java.lang.Enum class by default, in the above code,Shrubber is an enumeration class, GROUND, crawling, hanging as an instance of this enum class, because Shrubber has been inherited from the Java.lang.Enum class, so it cannot inherit other enum classes;
In addition, when using enumerations, you might consider using static import enumeration types, eliminating the use of enumeration types to decorate enumeration instances;
Adding a method to an enum
An enum is basically a generic class that can be added to a method, even the main method;
In the following code, add a constructor and GetDescription method inside the enumeration class, and note that when you define your own method, you need to add a semicolon after the last one in the enum instance sequence, and define the enum instance before defining the method;
Packageenumerated; Public enumOzwitch {//Instances must be defined first, before methods:West ("Miss Gulch, aka the Wicked Witch of the West"), North ("Glinda, the good Witch of the North"), EAST ("Wicked Witch of the East, wearer of the Ruby slippers, crushed by Dorothy ' s house"), South ("Good by inference, but missing"); PrivateString description; //Constructor must be package or private access: PrivateOzwitch (String description) { This. Description =description; } PublicString getdescription () {returndescription; } Public Static voidMain (string[] args) { for(Ozwitch witch:OzWitch.values ()) System.out.println (Witch+ ": " +witch.getdescription ()); }}
Take another example, overriding the Enum class method, the following code, overriding the ToString method, capitalizing the name of the enumeration, and the remainder to lowercase:
Packageenumerated; Public enumSpaceship {SCOUT, CARGO, TRANSPORT, CRUISER, Battleship, mothership; Publicstring toString () {string id=name (); String Lower= id.substring (1). toLowerCase (); returnId.charat (0) +Lower; } Public Static voidMain (string[] args) { for(Spaceship S:values ()) {System.out.println (s); } }}
Using enumerations in switch
The following code is the use of enumerations in the switch statement, note that in the case statement, the enumeration instance is not required to write the enumeration type, forced write on (such as signal.red) compiler will error;
Packageenumerated;//Define an enum type:enumSignal {GREEN, YELLOW, RED,} Public classtrafficlight {Signal color=signal.red; Public voidChange () {Switch(color) {//Note that you don ' t has to say signal.red//In the case statement: CaseRed:color=Signal.green; Break; CaseGreen:color=Signal.yellow; Break; CaseYellow:color=signal.red; Break; } } PublicString toString () {return"The traffic light is" +color; } Public Static voidMain (string[] args) {TrafficLight T=NewTrafficLight (); for(inti = 0; I < 7; i++) {System.out.println (t); T.change (); } }}
About the values () method in enumerations
We already know that the enumeration class inherits the Java.lang.Enum class, but the view enum source code does not find the values() method, so where does the values() method come from?
The answer is that the values() method is a static method that is added by the compiler and is interested in using the reflection mechanism to view, as in the example above, you can write the following code to view:
Method[] methods = Signal. class . GetMethods ();
Reference: Java Programming Ideas--4
"Java Basics" enumeration