/**
* Effective Java Second Edition
* 30th: Use enum instead of int constant
*/
Import Java.util.HashMap;
Import Java.util.Map;
public class Enumtest {
/* Media operation */
Public final static int START = 1;
Public final static int PAUSE = 2;
Public final static int RESUME = 3;
Public final static int STOP = 4;
/* Return results */
Public final static int ret_ok = 1;
private static int playwithint (int fun)
{
Switch (fun)
{
Case START:
Case PAUSE:
Case RESUME:
Case STOP:
System.out.println ("Playwithint do" + fun);
Break
Default
}
return RET_OK;
}
public enum Playem
{
Start,pause,resume,stop//namespace so that he can duplicate the name
}
private static void Playwithenum (Playem fun)
{
Switch (fun)
{
Case START:
Case PAUSE:
Case RESUME:
Case STOP:
System.out.println ("Playwithenum do" + fun);
Break
Default
}
}
public enum Superplayem
{
Start (1, "Start"), PAUSE (2, "pause"), RESUME (3, "Resume"), STOP (4, "stop");
private final int code;
Private final String name;
Superplayem (int code, String name) {
This.code = code;
THIS.name = name;
}
@Override
Public String toString () {
Return "superplayem{" +
"Code=" + code +
", Name= ' + name + ' + ' +
‘}‘;
}
public void Doplay (String player)
{
Switch (This)
{
Case START:
Case PAUSE:
Case RESUME:
Case STOP:
System.out.println ("Player:" + player + "exec" + this.name ());
Break
Default
}
}
}
private static void Playwithsuperenum (Superplayem fun)
{
Switch (fun)
{
Case START:
Case PAUSE:
Case RESUME:
Case STOP:
Default
System.out.println ("Playwithsuperenum do" + fun);
}
}
public enum Superplayapplyem
{
Start (1, "start") {void Doplay (String player) {System.out.println ("Player:" + player + "exec START");},
Pause (2, "pause") {void Doplay (String player) {System.out.println ("Player:" + player + "exec PAUSE");},
Resume (3, "resume") {void Doplay (String player) {System.out.println ("Player:" + player + "exec RESUME");},
Stop (4, "stop") {void Doplay (String player) {System.out.println ("Player:" + player + "exec STOP");}};
private final int code;
Private final String name;
Superplayapplyem (int code, String name) {
This.code = code;
THIS.name = name;
}
@Override
Public String toString () {
Return "superplayem{" +
"Code=" + code +
", Name= ' + name + ' + ' +
‘}‘;
}
abstract void Doplay (String player);
}
public enum Mixenum
{
START ("400100", "1001"),
PAUSE ("400100", "1002"),
RESUME ("400100", "1003"),
STOP ("400100", "1004")
;
Private final String Basecode;
Private final String Followcode;
private static final map<string,mixenum> Map = new hashmap<string,mixenum> ();
static {
For (Mixenum mixEnum:MixEnum.values ()) {
Map.put (Mixenum.basecode+mixenum.followcode,mixenum);
}
}
Mixenum (String Basecode, String followcode) {
This.basecode = Basecode;
This.followcode = Followcode;
}
public static Mixenum Fromcode (String code)
{
return Map.get (code);
}
}
public static void Main (string[] args) {
/*p128-p134*/
Traditional C-Practices
Playwithint (START); Printing is not recognized
Playwithint (RET_OK); Compiler cannot error
Generic Java Enumeration
Playwithenum (Playem.start);
Playwithenum (RET_OK);
/**
* Custom Java Enumeration
*/
Superplayem AAA = Superplayem.start;
Playwithsuperenum (AAA);
/**
* Print All enumeration information
* If someone wants a copy of the interface document, it will be very useful
*/
For (Superplayem superPlayEm:SuperPlayEm.values ()) {
System.out.println (Superplayem);
}
/**
* This enumeration can be very powerful, set a lot of parameters for him, you can also define functions
* But doing this may add an enumeration, forgetting to add a case branch
*/
Aaa.doplay ("Camera");
/**
* Prevent forgetting to add case
* But this leads to more code, each with pros and cons.
*/
Superplayapplyem Superplayapplyem = Superplayapplyem.pause;
Superplayapplyem.doplay ("Camera");
/**
* Method of generating enumeration 1 valueOf
*/
Superplayem Superplayem = superplayem.valueof ("START");
System.out.println (Superplayem);
/**
* Method of generating enumeration 2 custom
*
*/
Mixenum mixenum = Mixenum.fromcode ("4001001002");
System.out.println (Mixenum);
}
}
Effective Java Second Edition Enum