当前位置:编程学习 > JAVA >>

Emun 枚举类型使用浅析——包括enumSet enumMap

啥也不说了,直接贴代码~记录一下,方便以后使用。
 
请执行TestEnum类查看效果~
 
1.Enum 类Color
[java]  
/** 
 *  
 */  
package hpf.test;  
  
import java.util.EnumMap;  
import java.util.EnumSet;  
  
/** 
 * Enum 类Color 
 * @author pengfei.hpf 
 *  
 */  
  
  
public enum Color implements ColorInterface {  
          
        /** 
         * 枚举类型定义,和构造函数相对应 
         */  
        RED("红色", 1),  
        GREEN("绿色", 2),  
        BLANK("白色", 3),  
        YELLOW("黄色", 4);  
          
        private String name;  
        private int index;  
  
        /**构造函数 
         * @param name 
         * @param index 
         */  
        private Color(String name, int index) {  
                this.name = name;  
                this.index = index;  
    }  
  
    /* (non-Javadoc) 
     * @see hpf.test.ColorInterface#getName() 
     */  
    @Override  
    public String getName() {  
        return this.name;  
    }  
  
    /* (non-Javadoc) 
     * @see hpf.test.ColorInterface#print() 
     */  
    @Override  
    public String print() {  
        String msg = "index:"+index+" name:"+name;  
        System.out.println(msg);  
        return msg;  
    }  
      
    public static final EnumSet<Color> enumSet =  EnumSet.allOf(Color.class);  
    public static final EnumMap<Color,String> enumMap=  new EnumMap<Color,String>(Color.class);  
      
}  
 
2.接口类
[java] 
/** 
 *  
 */  
package hpf.test;  
  
/** 
 * 接口类 
 * @author pengfei.hpf 
 * 
 */  
public interface ColorInterface {  
  
        /**得到名称枚举类型Color的名称 
         * @return 
         */  
        public String getName();  
          
        /**打印枚举类型的信息 
         * @return 
         */  
        public String print();  
}  
 
3.测试类
[java]  
<pre name="code" class="java">package hpf.test;  
  
import java.util.Iterator;  
  
/**Enum 类的使用示例 
 * @author pengfei.hpf 
 * 
 */  
public class TestEnum {  
    public static final int RED = 1 ;  
    public static final int GREEN = 2;  
  
    /** 
     * @param args 
     */  
  
    public static void main(String[] args) {  
    enumFunc();  
    enumSetFunc();  
    enumMapFunc();  
    }  
      
    /** 
     * 基本方法测试 
     */  
    public static void enumFunc(){  
        System.out.println("enumFunc:"+Color.RED.getName());          
        Color.YELLOW.print();  
    }  
      
    /** 
     * EnumSet里的所有Enum对象都是唯一的 
     */  
    public static void enumSetFunc(){  
    Iterator<Color> it = Color.enumSet.iterator();  
    while (it.hasNext()) {  
        Color c = (Color) it.next();  
        c.print();  
    }  
    System.out.println("enumSet:"+Color.enumSet.toString());  
    }  
      
    /** 
     * enumMap的测试方法,key必须为Enum 类型数据,value可以为任意类型 
     */  
    public static void enumMapFunc() {  
    Color.enumMap.put(Color.RED, "This is RED");  
    Color.enumMap.put(Color.GREEN, "I am RED");  
    Color.enumMap.put(Color.YELLOW, "Oh,you are too yellow");  
    for(Color color : Color.enumMap.keySet())  
    {  
              System.out.println("enumMap:"+Color.enumMap.get(color));  
    }  
    }  
}  
</pre>  
[java]  
  
 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,