通过enum实现枚举类

通过enum实现枚举类

  • A:案例演示
    • 通过enum实现枚举类
package com.heima.enums2;

public enum Week01 {
	MON, TUE, WED;
}
====================================================
package com.heima.enums2;

public enum Week02 {
	MON("星期一"), TUE("星期二"), WED("星期三");

	private String name;

	private Week02(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String toString() {
		return name;
	}
}
====================================================
package com.heima.enums2;

public enum Week03 {
	MON("星期一") {
		public void show() {
			System.out.println("星期一");
		}
	},
	TUE("星期二") {
		public void show() {
			System.out.println("星期二");
		}
	},
	WED("星期三") {
		public void show() {
			System.out.println("星期三");
		}
	};

	private String name;

	private Week03(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public abstract void show();

}
====================================================
package com.heima.enums2;

public class Demo01_Enum {

	public static void main(String[] args) {
		Week01 mon = Week01.MON;
		System.out.println(mon);
		
		Week02 mon = Week02.MON;
		System.out.println(mon.getName());

		Week03 mon = Week03.MON;
		mon.show();
	}
}

你可能感兴趣的:(JDK5新特性)