Thinking in Java Enumeration
All enumeration classes are subclasses of java. lang. Enum, so custom enumeration classes cannot inherit other classes.
Common enumeration methods:
Values ():
Is the static method added by the compiler to the custom enum class.
Ordinal ()
ValueOf
Package com. demo. enums;/*** @ author wobendiankun * 10:09:53 */public class EnumsTest {/*** @ param args */public static void main (String [] args) {valueList (); ordinal (); valueOf ("A"); // AvalueOf ("F"); // java. lang. illegalArgumentException: No enum const class com. demo. enums. gradeType. f} private static void valueOf (String type) {System. out. println (); System. out. println (GradeType. valueOf (type);} private static void ordinal () {System. out. println (); // gets the ordinal number of an enumerated CONSTANT for (GradeType type: GradeType. values () {System. out. print (type. ordinal () + "\ t"); // 01234} private static void valueList () {// obtain all enumerated values for (GradeType type: GradeType. values () {System. out. print (type + "\ t"); // ABCDE }}} enum GradeType {A, B, C, D, E}
Obtain all enumerated constants through the Class Object
private static void getValuesByClass() {for(GradeType type:GradeType.class.getEnumConstants()){System.out.print(type+"\t");//ABCDE}System.out.println();}
Add a new method for the enumeration class
Note:
1. It cannot inherit from an instance other than Enum as a common java class. 2. You must first define an enum instance and add a semicolon at the end of the Instance sequence. 3. Create a mission to private or default.
Package com. demo. enums; public class EnumMethodTest {enum GradeType {// enumeration instance A (85), B (75), C (65), D (59); private int score; private GradeType (int score) {this. score = score;} public int getScore () {return score;}/*** @ param args */public static void main (String [] args) {GradeType type = GradeType. valueOf ("A"); System. out. println ("score:" + type. getScore (); // score: 85 }}
A (): calls the enumerated constructor.
Organize enumeration Using Interfaces
Department A has two Project Teams B and C. department leaders hope to unify the exception codes and information thrown by each project team and make A unified plan. Each project team can expand its own exception codes.
Class diagram:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPGJsb2NrcXVvdGU + cjxwp?vcd4kpha + fuse + c=vcd4kpha + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> package com. demo. enums. orga;/** Exception Code interface * @ author wobendiankun * 10:45:23 */public interface DepAErrorCode {/** get error code * @ return */String getCode (); /** get error message * @ return */String getDesc ();}
ProjectBErrorCode
Package com. demo. enums. orga;/** Project Team B error code interface * @ author wobendiankun * 10:50:00 */public interface ProjectBErrorCode extends DepAErrorCode {}
ProjectCErrorCode
Package com. demo. enums. orga;/** Project Team C error code interface * @ author wobendiankun * 10:50:00 */public interface ProjectCErrorCode extends DepAErrorCode {}
ProjectBCode
Package com. demo. enums. orga;/*** @ author wobendiankun * 10:58:47 */public enum ProjectBCode implements ProjectBErrorCode {ADD_USER ("10001", "failed to add user"), DELETE_USER ("10002 ", "failed to delete user"); private String code; private String desc; private ProjectBCode (String code, String desc) {this. code = code; this. desc = desc ;}@ Overridepublic String getCode () {return this. code ;}@ Overridepublic String getDesc () {return this. desc ;}}
DepAException
Package com. demo. enums. orga;/*** @ author wobendiankun * 10:45:43 */public class DepAException extends RuntimeException {public DepAException (DepAErrorCode depAErrorCode) {super (getMsg (depAErrorCode ));} static String getMsg (DepAErrorCode depAErrorCode) {return "error code:" + depAErrorCode. getCode () + ", error message:" + depAErrorCode. getDesc ();}}
ErrorCodeTest
Package com. demo. enums. orga;/*** @ author wobendiankun * 10:58:40 */public class ErrorCodeTest {/*** @ param args */public static void main (String [] args) {add (); // com. demo. enums. orga. depAException: Error code: 10001, error message: failed to add user} private static void add () {new UserSerivce (). add () ;}} class UserSerivce {void add () {throw new DepAException (ProjectBCode. ADD_USER );}}
Enumeration definition abstract Method
Package com. demo. enums;/*** @ author wobendiankun * 11:13:19 */public class enum1_acttest {enum GradeType {A () {@ overrisponid print () {System. out. println ("excellent") ;}}, B () {@ overrisponid print () {System. out. println ("good") ;}}; // defines an abstract method abstract void print ();} public static void main (String [] args) {GradeType type = GradeType. b; type. print (); // good }}
EnumSet & EnumMap
public class EnumSetTest { /** * @param args */ public static void main(String[] args) { EnumSet
set= EnumSet.allOf( Type.class); print(set ); set=EnumSet .noneOf( Type.class); print(set ); set=EnumSet .of( Type.AA , Type. CC); print(set ); } static void print(EnumSet
set){ System.out .println(set); }}enum Type { AA,BB ,CC, DD,EE ,FF }
Public class EnumMapTest {static EnumMap
Map = new EnumMap
(Type. class); static void init () {map. put (Type. AA, new InvokeHandler () {@ Override public void handle () {System. out. println ("processed AA... ") ;}}); map. put (Type. CC, new InvokeHandler () {@ Override public void handle () {System. out. println ("processed CC... ") ;}}) ;}/ *** @ param args */public static void main (String [] args) {init (); map. get (Type. CC ). handle (); map. get (Type. AA ). handle (); map. get (Type. DD ). handle () ;}} interface InvokeHandler {void handle ();}