First, enumeration definitions
Enumeration cannot be defined in a static method
Generic custom enum types are subtypes of enum Enumtype
It has two static methods (class methods) by default: Values () and valueof ()
VALUES () returns an array of instances of type Enumtype
ValueOf () Returns an instance of the Enumtype type
Two
Example: Custom color enum type
color{White, BLACK, GRAY}
Color C = Color.gray;
So:
Color.valueof ("BLACK") = = Color.Black = = Color.values () [1]
Because they all return a color type
c.tostring () = = C.name ()
Because they are returning a string
Iii. constructors and member variables for enumerations
The constructor for an enumeration can only be private
The member variable of the enumeration is not restricted, but if the method defined in the enumeration instance needs access to the member variable in the enumeration type, the member variable is at least protected, otherwise it cannot inherit
Package newpack17;interface d{public String getdescription (); public int Geti ();} Public enum Anyenum implements d{constants_a{public String getdescription () {return "I am enum member a"; } public int Geti () {return i; }}, constants_b{public String getdescription () {return ' I am enum member B '; } public int Geti () {return i; }}, Constants_e ("enumeration member E", "n") {public String getdescription () {return this.str; } public int Geti () {return this.it; }}, Constants_c, constants_d{public String getdescription () {return "I am enum member D"; } public int Geti () {return i; } }; Private Anyenum (String str,int i) {this.str = str; this.it=i; } private Anyenum () {} public String getdescription () {return "I am enum member xxx"; } public int Geti () {return 10000; } public int it; Protected is OK, but cannot be private protected String str; private static int i = 5; Static member public protected,private all lines public static void main (string[] args) {for (int i = 0; I<anyenum.values () . length; i++) {System.out.println (Anyenum.values () [i]+] calls the GetDescription () method as: "+anyenum.values () [I].getdescription ()]; The System.out.println (Anyenum.values () [i]+ "Call Geti () method is:" +anyenum.values () [I].geti ()]; } }}
About Java enumerations