• JDK1.5 you can then use enum keyword to define an enumeration type with the following syntax structure:
[public] enum Enumeration Name [implements interface list ] {
Enumerating Objects 1 [, Enumeration Object 2] [,... ];
[ member variable 1; ]
[ member variable 2; ]
[ ... ]
[( Static or non-static ) block of code
[ construction method 1]
[ construction method 2]
[ ... ]
[ general method 1]
[ general method 2]
[ ... ]
[ abstract method 1]
[ abstract method 2]
[ ... ]
}
public enum Color {red (1, "red"), Green (2, "green"), Blue (3, "Blue");p rivate int value;private String label;private Color (int va Lue, String label) {this.value = Value;this.label = label;} public int GetValue () {return value;} Public String Getlabel () {return label;}}
You can define abstract objects directly in an enumeration at this point, each enumeration object needs to implement the abstract method, as in the following example
Enum Color {red {public string Getlabel () {return ' red color ';}},green {public string Getlabel () {return ' green color ';}}; Public abstract String Getlabel ();} public class Test {public static void main (string[] args) {for (Color color:Color.values ()) {String label = Color.getlab El (); SYSTEM.OUT.PRINTLN (label);}}}
• enumerate nouns . enumeration of Image : Gets a specified enumeration object, as in the following example:
static t[] values () : Gets all enumerated objects in the custom enumeration, as in the following example
• Enumeration types can be used in switch, as in the following example:
• Theenum class is an abstract class that is the parent of the Java Language enumeration type, that is, the enum keyword custom enumeration type is equivalent to defining a Inheritance Java.lang.Enum the child of the abstract class class.
•Enum class construction method:
Øprotected Enum(String name, int ordinal) : The construction method receives two parameters, one that represents the name of the enumeration, and another that represents the ordinal of the enumeration;