枚举的简单应用(一)
枚举类
1 package com.yeepay.sxf.euma; 2 /** 3 * 表示星期的枚举 4 * @author sxf 5 * 6 */ 7 public enum Week { 8 Monday, 9 Tuesday , 10 Wednesday, 11 Thursday , 12 Friday , 13 Saturday, 14 Sunday, 15 16 }
测试类
1 package com.yeepay.sxf.euma; 2 3 4 public class Test { 5 public static void main(String[] args) { 6 test1(); 7 } 8 9 10 public static void test1(){ 11 //将枚举转换成==>字符串 12 String ad=Week.Friday.name(); 13 System.out.println("将枚举转换成==>字符串:"+ad); 14 15 //将字符串转换成==>枚举 16 String mon="Friday"; 17 Week mo=Week.valueOf(mon); 18 System.out.println("将字符串转换成==>枚举:"+mo); 19 20 //将枚举类内的枚举转换成==>数组 21 Week[] eWeeks=Week.values(); 22 for (int i = 0; i < eWeeks.length; i++) { 23 System.out.println("将枚举类内的枚举转换成==>数组:"+eWeeks[i]); 24 } 25 } 26 }
测试结果
将枚举转换成==>字符串:Friday
将字符串转换成==>枚举:Friday
将枚举类内的枚举转换成==>数组:Monday
将枚举类内的枚举转换成==>数组:Tuesday
将枚举类内的枚举转换成==>数组:Wednesday
将枚举类内的枚举转换成==>数组:Thursday
将枚举类内的枚举转换成==>数组:Friday
将枚举类内的枚举转换成==>数组:Saturday
将枚举类内的枚举转换成==>数组:Sunday