hoji-real

java的Enum枚举类型终于在j2se1.5出现了。之前觉得它只不过是鸡肋而已,可有可无。毕竟这么多年来,没有它,大家不都过得很好吗?今日看 《Thinking in Java》4th edition,里面有一句话“有时恰恰因为它,你才能够"优雅而干净"地解决问题。优雅与清晰很重要,正式它们区别了成功的解决方案与失败的解决方案。 而失败的解决方案就是因为其他人无法理他。"使用Enum枚举类型,可以将以前笨拙的代码变得优雅简单?但是,我同时也在思考另外一个问题,使用新的技 术,会不会给技术人员带来更多的负担呢?

"学习新版语言的一个危险就是疯狂使用新的语法结构"

先学习一下enum的简单应用,以下简洁的代码已经包括enum所提供的绝大部分功能。

1.enum的应用,包括定义,遍历,switch,enumset,enummap等

Java代码  收藏代码
  1. package com.janeky.enumtest;     
  2. import java.util.EnumMap;     
  3. import java.util.EnumSet;     
  4.     
  5. /**   
  6.  * Java枚举类型enum使用详解   
  7.  * @version 2009/9/5   
  8.  * @author janeky   
  9.  * @mail rojaneky@gmail.com   
  10.  *   
  11.  */    
  12. public class EnumTest {     
  13.     
  14.     //定义一个enum枚举类型,包括两个实例ON,OFF     
  15.     public enum State {     
  16.         ON, OFF     
  17.     };     
  18.     
  19.     //测试函数     
  20.     public static void main(String[] args) {     
  21.         //直接变量enum     
  22.         for (State s : State.values())     
  23.             System.out.println(s.name());     
  24.     
  25.         //switch与enum的结合使用     
  26.         State switchState = State.OFF;     
  27.         switch (switchState) {     
  28.         case OFF:     
  29.             System.out.println("OFF");     
  30.             break;     
  31.         case ON:     
  32.             System.out.println("ON");     
  33.             break;     
  34.         }     
  35.     
  36.         //EnumSet的使用     
  37.         EnumSet stateSet = EnumSet.allOf(State.class);     
  38.         for (State s : stateSet) {     
  39.             System.out.println(s);     
  40.         }     
  41.     
  42.         //EnumMap的使用     
  43.         EnumMap stateMap = new EnumMap(     
  44.                 State.class);     
  45.         stateMap.put(State.ON, "is On");     
  46.         stateMap.put(State.OFF, "is off");     
  47.         for (State s : State.values()) {     
  48.             System.out.println(s.name() + ":" + stateMap.get(s));     
  49.         }     
  50.     }     
  51.     
  52. }    

 

Java代码  收藏代码
  1. package com.janeky.enumtest;  
  2. import java.util.EnumMap;  
  3. import java.util.EnumSet;  
  4.   
  5. /** 
  6.  * Java枚举类型enum使用详解 
  7.  * @version 2009/9/5 
  8.  * @author janeky 
  9.  * @mail rojaneky@gmail.com 
  10.  * 
  11.  */  
  12. public class EnumTest {  
  13.   
  14.  //定义一个enum枚举类型,包括两个实例ON,OFF  
  15.  public enum State {  
  16.   ON, OFF  
  17.  };  
  18.   
  19.  //测试函数  
  20.  public static void main(String[] args) {  
  21.   //直接变量enum  
  22.   for (State s : State.values())  
  23.    System.out.println(s.name());  
  24.   
  25.   //switch与enum的结合使用  
  26.   State switchState = State.OFF;  
  27.   switch (switchState) {  
  28.   case OFF:  
  29.    System.out.println("OFF");  
  30.    break;  
  31.   case ON:  
  32.    System.out.println("ON");  
  33.    break;  
  34.   }  
  35.   
  36.   //EnumSet的使用  
  37.   EnumSet stateSet = EnumSet.allOf(State.class);  
  38.   for (State s : stateSet) {  
  39.    System.out.println(s);  
  40.   }  
  41.   
  42.   //EnumMap的使用  
  43.   EnumMap stateMap = new EnumMap(  
  44.     State.class);  
  45.   stateMap.put(State.ON, "is On");  
  46.   stateMap.put(State.OFF, "is off");  
  47.   for (State s : State.values()) {  
  48.    System.out.println(s.name() + ":" + stateMap.get(s));  
  49.   }  
  50.  }  
  51.   
  52. }  



为每个enum实例定义不同的方法

Java代码  收藏代码
  1. package com.janeky.enumtest;     
  2.     
  3. public enum TestEnumMathod {     
  4.     //为每个enum实例添加不同的实现方法     
  5.     SAMPLE1 {     
  6.         String getInfo() {     
  7.             return "SAMPLE1";     
  8.         }     
  9.     },     
  10.     SAMPLE2{     
  11.         String getInfo()     
  12.         {     
  13.             return "SAMPLE2";     
  14.         }     
  15.     };     
  16.     abstract String getInfo();     
  17.          
  18.     //测试     
  19.     public static void main(String args[])     
  20.     {     
  21.         for(TestEnumMathod method:values())     
  22.         {     
  23.             System.out.println(method.getInfo());     
  24.         }     
  25.     }     
  26.          
  27. }    


Java代码  收藏代码
  1. package com.janeky.enumtest;  
  2.   
  3. public enum TestEnumMathod {  
  4.  //为每个enum实例添加不同的实现方法  
  5.  SAMPLE1 {  
  6.   String getInfo() {  
  7.    return "SAMPLE1";  
  8.   }  
  9.  },  
  10.  SAMPLE2{  
  11.   String getInfo()  
  12.   {  
  13.    return "SAMPLE2";  
  14.   }  
  15.  };  
  16.  abstract String getInfo();  
  17.    
  18.  //测试  
  19.  public static void main(String args[])  
  20.  {  
  21.   for(TestEnumMathod method:values())  
  22.   {  
  23.    System.out.println(method.getInfo());  
  24.   }  
  25.  }  
  26.    
  27. }  



以下内容可能有些无聊,但绝对值得一窥
1.

Java代码  收藏代码
  1. public class State {  
  2.   public static final int ON = 1;  
  3.   public static final Int OFF= 0;  
  4. }  


有什么不好了,大家都这样用了很长时间了,没什么问题啊。
首先,它不是类型安全的。你必须确保是int
其次,你还要确保它的范围是0和1
最后,很多时候你打印出来的时候,你只看到 1 和0 ,

但其没有看到代码的人并不知道你的企图
so,抛弃你所有旧的public static final常量吧

2.可以创建一个enum类,把它看做一个普通的类。除了它不能继承其他类了。(java是单继承,它已经继承了Enum),
可以添加其他方法,覆盖它本身的方法

3.switch()参数可以使用enum了

4.values()方法是编译器插入到enum定义中的static方法,所以,当你将enum实例向上转型为父类Enum 是,values()就不可访问了。解决办法:在Class中有一个getEnumConstants()方法,所以即便Enum接口中没有 values()方法,我们仍然可以通过Class对象取得所有的enum实例

5.无法从enum继承子类,如果需要扩展enum中的元素,在一个接口的内部,创建实现该接口的枚举,以此将元素进行分组。达到将枚举元素进行分组。

6.使用EnumSet代替标志。enum要求其成员都是唯一的,但是enum中不能删除添加元素。

7.EnumMap的key是enum,value是任何其他Object对象。

8.enum允许程序员为eunm实例编写方法。所以可以为每个enum实例赋予各自不同的行为。

9.使用enum的职责链(Chain of Responsibility) .这个关系到设计模式的职责链模式。以多种不同的方法来解决一个问题。然后将他们链接在一起。当一个请求到来时,遍历这个链,直到链中的某个解决方案能够处理该请求。

10.使用enum的状态机

11.使用enum多路分发

-------------------------

Java代码  收藏代码
  1. public class EnumTest {  
  2.   public enum PayMethod {  
  3.         货到7天, 货到15天 , 月结30天, 月结60天, 月结90天  
  4.     }  
  5.       
  6.     public enum Currency {  
  7.         RMB, USD  
  8.     }  
  9. // 以下测试Enum PayMethod 和 Enum Currency  
  10.     public static void main(String[] args) {  
  11.           
  12.         // 1. 直接变量enum  
  13.         for (PayMethod pm : PayMethod.values())   
  14.             System.out.println("直接变量 支付方式 ->" + pm);  
  15.           
  16.         // 2. EnumMap  
  17.         Map<PayMethod, String> payMethodMap = new EnumMap<PayMethod, String>(PayMethod.class);  
  18.         payMethodMap.put(PayMethod.月结30天, " to support。");  
  19.         payMethodMap.put(PayMethod.月结90天, " to unsupport。");  
  20.         for (PayMethod pm : PayMethod.values()) {  
  21.             System.out.println("Pay Methos -> " + pm.name() + payMethodMap.get(pm));  
  22.         }  
  23.           
  24.         // 3. EnumSet  
  25.         Set<Currency> currencys = EnumSet.allOf(Currency.class);  
  26.         for (Currency c : currencys) {  
  27.             System.out.println("币种 -> " + c);  
  28.         }  
  29. }  


引自: http://tech.e800.com.cn/articles/2009/97/1252286198897_1.html

分类:

技术点:

相关文章:

  • 2021-10-08
  • 2021-09-24
  • 2021-09-24
  • 2018-06-06
  • 2021-12-06
  • 2021-12-16
猜你喜欢
  • 2021-12-16
  • 2021-08-23
  • 2019-08-10
  • 2021-12-11
  • 2021-07-29
  • 2021-12-06
  • 2019-09-15
相关资源
相似解决方案