enum使用

package chow;

import java.util.EnumMap;
import java.util.EnumSet;

import junit.framework.TestCase;

public class EnumUse extends TestCase{
	public enum State{
		ON, OFF
	}
	
	/**
	 * enum遍历,enum与switch,
	 * enumSet,enumMap
	 * 
	 * @author chow 2010-8-26 下午02:46:28
	 */
	public void testBaseEnum(){
		//遍历enum
		System.out.println("traversal enum");
		for(State s : State.values()){
			System.out.println(s.name());
		}
		
		//enum与switch
		System.out.println("enum and switch:");
		State tmpState = State.OFF;
		switch(tmpState){
		case ON : 
			System.out.println("ON");
			break;
		case OFF :
			System.out.println("OFF");
			break;
		}
		
		//enumset
		System.out.println("EnumSet:");
		EnumSet enumset = EnumSet.allOf(State.class);
		for(State s : enumset){
			System.out.println(s);
		}
		
		//EnumMap
		System.out.println("EnumMap:");
		EnumMap map = new EnumMap(State.class);
		map.put(State.OFF, " is off");
		map.put(State.ON, " is on");
		for(State s : State.values()){
			System.out.println(s.name() + map.get(s));
		}
	}
}

 

你可能感兴趣的:(编程)