Java enum 枚举类型使用举例

/** Java 枚举类型 用法介绍**/
public class Test_11{

	//1.枚举类型的创建:创建了一个名字为ShootingMode(拍摄模式)的枚举类型,它具有5个具名值
	public enum ShootingMode{
		COMMON, SMART, HDR, SMILE, SELF_TIMER
	}
	
	public static void main(String[] args){
	
		//2.使用enum: 创建该类型的引用,并将其赋值给某个实例
		ShootingMode mode = ShootingMode.SMART;
		
		//3.toString():编译器自动添加的特性。很方便的显示实例的名称
		System.out.println(mode);	//Output: SMART
		
		//4.ordinal(): 表示某个特定enum常量的声明顺序
		System.out.println(mode.ordinal());	//Output: 1
		
		//5.static values(): 也是编译器添加的特性,按照enum常量的声明顺序,组成数组
		for(ShootingMode m: ShootingMode.values()){
			System.out.println(m); 
		}
		
		//6.在switch语句中的应用
		Test_11
			t1 = new Test_11(ShootingMode.COMMON),
			t2 = new Test_11(ShootingMode.SMILE),
			t3 = new Test_11(ShootingMode.SELF_TIMER);
			t1.mode();
			t2.mode();
			t3.mode();
		
	}
	
	private ShootingMode shootingMode;
	public Test_11(ShootingMode m){
		shootingMode = m;
	}
	void mode(){
		System.out.print("shootingMode = ");
		switch(shootingMode){
			case COMMON:
				System.out.println("普通模式");
				break;
			case SMART:
				System.out.println("智能模式");
				break;
			case HDR:
				System.out.println("逆光模式");
				break;
			case SMILE:
				System.out.println("笑脸模式");
				break;
			case SELF_TIMER:
				System.out.println("自拍模式");
				break;
			default:
		}
	}
}

你可能感兴趣的:(java,timer,String,Class,编译器)