一、 枚举类的说明
枚举类的理解:类的对象只有有限个,确定的。 当需要定义一组常量时,强烈建议使用枚举类 如果枚举类只一个对象,则可以作为单例模式的实现方式。
二、如何定义枚举类
① 自定义枚举类
代码语言:javascript复制class Season{
// 1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
// 私有化的构造器,并给对象赋值
private Season(String seasonName, String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
// 3.提供当前枚举类的多个对象:public static final的
public static final Season SPRING = new Season("春天","春暖花开");
public static final Season SUMMER = new Season("夏天","夏日炎炎");
public static final Season AUTUMN = new Season("秋天","秋高气爽");
public static final Season WINTER = new Season("冬天","冰天雪地");
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season{"
"seasonName='" seasonName '''
", seasonDesc='" seasonDesc '''
'}';
}
}
代码语言:javascript复制public class SeasonTest {
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
System.out.println(autumn); // Season{seasonName='秋天', seasonDesc='秋高气爽'}
}
}
② JDK5.0 新增使用 enum 定义枚举类。
代码语言:javascript复制// 使用enum关键枚举类
enum Season1{
// 1.提供当前枚举类的对象,多个对象之间用“,”隔开,末尾对象";"结束
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
// 2.声明Season对象的属性:private final修饰
private final String SeasonName;
private final String SeasonDesc;
// 3.私有化构造器,并给对象赋值
private Season1(String SeasonName, String SeasonDesc){
this.SeasonName = SeasonName;
this.SeasonDesc = SeasonDesc;
}
// 4.获取枚举类对象的属性
public String getSeasonName() {
return SeasonName;
}
public String getSeasonDesc() {
return SeasonDesc;
}
@Override
public String toString() {
return "Season1{"
"SeasonName='" SeasonName '''
", SeasonDesc='" SeasonDesc '''
'}';
}
}
代码语言:javascript复制public class SeasonTest2 {
public static void main(String[] args) {
Season1 season = Season1.AUTUMN;
System.out.println(season); // Season1{SeasonName='秋天', SeasonDesc='秋高气爽'}
}
}
③ emum 定义枚举类之后,枚举类常用方法:(继承于 java.lang.Enum 类)
- value() 返回所有的枚举类对象的构成的数组
- valueOf(String objName):返回枚举类中对象名是 objName 的对象。
interface Info{
void show();
}
// 使用enum关键字枚举类
enum Season2 implements Info{
//1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束
SPRING("春天","春暖花开"){
@Override
public void show() {
System.out.println("春天在那里");
}
},
SUMMER("夏天","夏日炎炎"){
@Override
public void show() {
System.out.println("宁夏");
}
},
AUTUMN("秋天","秋高气爽"){
@Override
public void show() {
System.out.println("秋天不回来");
}
},
WINTER("冬天","冰天雪地"){
@Override
public void show() {
System.out.println("大约在冬季");
};
};
private final String SeasonName;
private final String SeasonDesc;
private Season2(String SeasonName , String SeasonDesc){
this.SeasonName = SeasonName;
this.SeasonDesc = SeasonDesc;
}
public String getSeasonName() {
return SeasonName;
}
public String getSeasonDesc() {
return SeasonDesc;
}
@Override
public String toString() {
return "Season2{"
"SeasonName='" SeasonName '''
", SeasonDesc='" SeasonDesc '''
'}';
}
}
代码语言:javascript复制public class SeasonTest3 {
public static void main(String[] args) {
Season2 season = Season2.AUTUMN;
System.out.println(season.toString()); //Season2{SeasonName='秋天', SeasonDesc='秋高气爽'}
season.show(); // 秋天不回来
// value 返回所有的枚举类对象的构成的数组
Season2[] values = season.values();
// for (Season2 value : values) {
// System.out.println(value);
// value.show();
// }
for(int i = 0 ; i <values.length; i ){
System.out.println(values[i]);
values[i].show();
}
System.out.println("*************************************************");
Thread.State[] values1 = Thread.State.values();
for(int i = 0 ; i < values1.length; i ){
System.out.println(values1[i]);
}
System.out.println("***************************************");
// valueOf(String objName):返回枚举类中对象名是objName的对象。
Season2 spring = Season2.valueOf("SPRING");
System.out.println(spring); // Season2{SeasonName='春天', SeasonDesc='春暖花开'}
}
运行结果:
- value()