First understand the nature of enumerations in Java.
Everything in the Java World is a class, and here's an example to explain the nature of the enum:
Packagecn.xnchall.enumeration; Public classGrade { Public voidTest () {print (grade. A); } Public voidPrint (grade g) {//print A B C D E }}/*class grade{Private Grade () {} public static final grade A = new Grade (); public static final Grade B = new Grade (); public static final Grade C = new Grade (); public static final Grade D = new Grade (); public static final Grade E = new Grade ();}*/enumgrade{//class a,b,c,d,e; Object}
The grade class and the Grade enumeration type work the same way, which means that the enumeration type is a more rigorous class. It does not allow the new grade class, because the underlying implementation is private. To implement the new operation externally, it must be implemented inside the grade.
The following is a formal introduction enumeration!
1. Defining constructors, methods, and fields for enumerations
Packagecn.xnchall.enumeration; Public classDome { Public Static voidTest () {print (grade. A); } Public Static voidPrint (grade g) {//print A B C D ESystem.out.println (G.getvalue ()); } Public Static voidMain (string[] args) {test (); }}/*class grade{Private Grade () {} public static final grade A = new Grade (); public static final Grade B = new Grade (); public static final Grade C = new Grade (); public static final Grade D = new Grade (); public static final Grade E = new Grade ();}*/enumgrade{//class A 100-90 B 89-80 C 79-70 D 69-60 E 59-0A ("100-90"), B ("89-80"), C ("79-70"), D ("69-60"), E ("59-0") ; PrivateString value;//the score for each field PrivateGrade (String value) { This. Value =value; } PublicString GetValue () {return This. Value; }}
The result that will be executed above is: 100-90
Not to be continued
Will be added: enumerations with abstract methods
Enumeration of Java 2