Java高级特性–枚举(Enum)

枚举就是预先定义一组常量,并作为一个新的类型,就是类似一个常量类。从定义上来看,枚举很想特殊的class,实际上枚举的声明定义就是一个类。先看看枚举类与常量类的区别。
枚举类:

package enumdemo;
public enum Week {
	monday,
	tuesday,
	wedensday,
	thursday,
	friday,
	saturday,
	sunday
}

常量类:

package enumdemo;
 
public class StaticClass {
	public static final int monday=1;
	public static final int tuesday=2;
	public static final int wedensday=3;
	public static final int thursday=4;
	public static final int friday=5;
	public static final int saturday=6;
	public static final int sunday=7;
}


看看调用结果

 
package enumdemo;
public class EnumTest {
	private static int dayOfClass=StaticClass.monday;
	private static Week dayOfWeek=Week.monday;
	/**
	 * @param args
	 */
	public void setDayOfClass(int dayOfClass) {
		this.dayOfClass=dayOfClass;
	}
	public void setDayOfWeek(Week dayOfWeek) {
		this.dayOfWeek=dayOfWeek;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EnumTest et=new EnumTest();
		System.out.println(dayOfClass);
		et.setDayOfClass(StaticClass.tuesday);
		System.out.println(dayOfClass);
		et.setDayOfClass(8);//可以设置超出静态类范围的至
		System.out.println(dayOfClass);
 
		et.setDayOfWeek(Week.tuesday);//只能设置Week内的值
		System.out.println(dayOfWeek);
		System.out.println(Week.friday.toString());
	}
 
}

从上面运行结果可以知道,常量类在再次赋值的时候,没有约束值的范围,所有就有了et.setDayOfClass(8)的结果,而且在编译和运行时没有报错,这是因为setDayOfClass的参数是整数。而在et.setDayWeek(Week dayOfWeek)中则不允许传入参数在定义的Week范围之外的。
另外需要说明一点的是,枚举中定义的元素自动有public static final三种属性。枚举覆盖了toString方法,所以在:

System.out.println(Week.friday.toString())

其运行结果是friday。
Enum API举例
1.values() 举得枚举对象的类型数组

	for(Week w:dayOfWeek.values())
		System.out.println(w);

输出Week中的所有元素
2.valueOf() 取出一个枚举值

	System.out.println(dayOfWeek.valueOf("sunday"));

输出值为“Sunday”
3.Enum.valueOf(class,String) 是静态方法,取出某个枚举类的某个值

	System.out.println(Enum.valueOf(Week.class,"tuesday");

4.在枚举类里可以使用switch语句
再来看看改造过的枚举类Week.java

package enumdemo;
 
public enum Week {
	monday,
	tuesday,
	wedensday,
	thursday,
	friday,
	saturday,
	sunday;
 
	public String printInfo(Week w) {
		String day = null;
		switch(w) {
			case monday:day="星期一";break;
			case tuesday:day="星期二";break;
			case wedensday:day="星期三";break;
			case thursday:day="星期四";break;
			case friday:day="星期五";break;
			case saturday:day="星期六";break;
			case sunday:day="星期日";break;
		}
		return day;
	}
}

再EnumTest中调用printInfo(Week w)方法

	System.out.println(et.dayOfWeek.printInfo(dayOfWeek.friday));

输出:星期五
switch(w)中w的类型为Week,就是enum类型
枚举类构造函数和方法的使用,再来看看改造过的Week枚举类

package enumdemo;
 
public enum Week {
	monday,
	tuesday,
	wedensday,
	thursday,
	friday,
	saturday,
	sunday;
 
	String name=null;
	Week() {
 
	}
	Week(String name) {
		this.name=name;
	}
 
	public void printInfo1() {
		for(Week w:this.values())
			System.out.println(w);
	}
	public String printInfo(Week w) {
		String day = null;
		switch(w) {
			case monday:day="星期一";break;
			case tuesday:day="星期二";break;
			case wedensday:day="星期三";break;
			case thursday:day="星期四";break;
			case friday:day="星期五";break;
			case saturday:day="星期六";break;
			case sunday:day="星期日";break;
		}
		return day;
	}
}

Week有两个构造函数,有参和无参,并有两个方法。
在枚举类里,构造函数只能是private、无修饰符两种,
当定义一个枚举类时,就已经确定了他的实例对象,像Week有7个对象
枚举类的实例都是同时构造的,当初始化一个枚举类时,就会构造其中的所有元素
枚举常用的特性就是这些,做个笔记,共日后参考。O(∩_∩)O~

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Go back to top