The enumeration classes are new after Java 5, can be used to encapsulate constants, and can also provide some methods for the use of constants.
Defines the syntax for an enumeration class:
Public enum enumname{ //must be placed on the first row of member 1 (A, B ... ), member 2 (A, B ... ), member 3 (A, b ...). )........; //a,b can not Private Type A; Private Type B; ...... Private enumname (Type a,type b ...) { a=A; b=b; ..... } about A, b ..... The Get and set methods }
Get an enumeration member in an enumeration class:
Enumerates the class names. Enumeration members;
By: Enumerates the class names. VALUES () can get all the enumeration members in the enumeration class;
enum aa{ a,b,c;}
To traverse all the elements of the output, then:
for (AA s:aa.values () ) System.out.println (s);
The result is:
A
B
C
Write a Wheher.java class with the complete code as follows:
enumAA {YES (2), NO (4); Private intvalue; PrivateAA (intI//You can also omit private{Value=i; } Public intGetValue () {returnvalue; } Public voidSetValue (intvalue) { This. Value =value; } } Public classwheher{ Public Static voidMain (String args[]) {System.out.println ("Output the value of the member No in the enumeration class----" +aa.no); System.out.println ("\ n---Call function isyes ()--"); System.out.println (Isyes (AA. YES)); System.out.println (Isyes (aa.no)+ "\ n"); System.out.println ("View values replaced by enumeration constants---" +AA. Yes.getvalue ()); //set the value of an enumeration constantAa. Yes.setvalue (25); System.out.println ("\ n Change the value of Yes to----" +AA. Yes.getvalue ()); } Public Static BooleanIsyes (AA a) {if(AA. Yes.equals (a))return true; Else return false; }}
The result of the operation is:
For an enumeration class, refer to: http://www.cnblogs.com/sister/p/4700702.html
Java enum Class (enum)