java_day24_自定义枚举类

package com.itheima.domain;

public class TestSeason {

	public static void main(String[] args) {
		Season spring = Season.SPRING;
		System.out.println(spring);
	}
}

/**
 * 自定义枚举类
 */
class Season {
	// 1.提供类的属性,声明为private final
	private final String seasonName;
	private final String seasonDesc;

	// 2.声明为final的属性,在构造器中初始化
	private Season(String seasonName, String seasonDesc) {
		this.seasonName = seasonName;
		this.seasonDesc = seasonDesc;
	}

	// 3.通过公共的方法来调用属性
	public String getSeasonName() {
		return seasonName;
	}

	public String getSeasonDesc() {
		return seasonDesc;
	}

	// 4.创建枚举类的对象
	public static final Season SPRING = new Season("spring", "春暖花开");
	public static final Season SUMMER = new Season("summer", "夏日炎炎");
	public static final Season FALL = new Season("fall", "秋高气爽");
	public static final Season WINTER = new Season("winter", "白雪皑皑");

	@Override
	public String toString() {
		return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
	}

}
package com.itheima.domain;

public class TestSeason1 {

	package com.itheima.domain;

public class TestSeason {
	public static void main(String[] args) {
		//1.获取所有枚举类对象
		Season[] seasons = Season.values();
		for (int i = 0; i < seasons.length; i++) {
			System.out.println(seasons[i]);
		}
		//2.大小写必须和枚举类的对象一样
		String str = "SPRING";
		Season season = Season.valueOf(str);
		System.out.println(season);
	}
}

/**
 * 枚举类
 */
enum Season {

	//4.创建枚举类的对象
	SPRING("spring", "穿暖花开"),
	SUMMER("summer", "夏日炎炎"),
	FALL("fall", "秋高气爽"),
	WINTER("winter", "白雪皑皑");
	
	// 1.提供类的属性,声明为private final
	private final String seasonName;
	private final String seasonDesc;

	// 2.声明为final的属性,在构造器中初始化
	private Season(String seasonName, String seasonDesc) {
		this.seasonName = seasonName;
		this.seasonDesc = seasonDesc;
	}

	// 3.通过公共的方法来调用属性
	public String getSeasonName() {
		return seasonName;
	}

	public String getSeasonDesc() {
		return seasonDesc;
	}
	
	@Override
	public String toString() {
		return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
	}
}
package com.itheima.domain;

/**
 * 枚举实现一个接口
 *
 */
public class TestSeason {
	public static void main(String[] args) {
		// 1.获取所有枚举类对象
		Season[] seasons = Season.values();
		for (int i = 0; i < seasons.length; i++) {
			System.out.println(seasons[i]);
		}
		// 2.大小写必须和枚举类的对象一样
		String str = "SPRING";
		Season season = Season.valueOf(str);
		System.out.println(season);

		System.out.println("=====================");

		Season fall = Season.FALL;
		fall.show();
	}
}

interface Info {
	void show();
}

/**
 * 枚举类
 */
enum Season implements Info {

	// 4.创建枚举类的对象
	SPRING("spring", "穿暖花开") {
		public void show() {
			System.out.println("春天在哪里?");
		}
	},
	SUMMER("summer", "夏日炎炎") {
		public void show() {
			System.out.println("夏日旭阳");
		}
	},
	FALL("fall", "秋高气爽") {
		public void show() {
			System.out.println("秋天是一个分手的季节");
		}
	},
	WINTER("winter", "白雪皑皑") {
		public void show() {
			System.out.println("冬天里的一把火");
		}
	};

	// 1.提供类的属性,声明为private final
	private final String seasonName;
	private final String seasonDesc;

	// 2.声明为final的属性,在构造器中初始化
	private Season(String seasonName, String seasonDesc) {
		this.seasonName = seasonName;
		this.seasonDesc = seasonDesc;
	}

	// 3.通过公共的方法来调用属性
	public String getSeasonName() {
		return seasonName;
	}

	public String getSeasonDesc() {
		return seasonDesc;
	}

	@Override
	public String toString() {
		return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
	}
}

 

你可能感兴趣的:(javaSE)