注意:枚举是单例的,单例要保证私有化构造器。
举例如下:
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
public static final Season SPRING = new Season("spring", "春暖花开");
public static final Season SUMMER = new Season("summer", "夏日炎炎");
public static final Season AUTUMN = new Season("autumn", "秋高气爽");
public static final Season WINTER = new Season("winter", "白雪皑皑");
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
public void show() {
System.out.println("这是一个季节");
}
}
public class TestSeason {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());
}
}
将对象放到最前面,并且不需要定义对象的其他属性。 使用 enum 类来定义枚举类,可以使用下述两种方法来返回值。
改造一下上述代码:
enum Season {
SPRING("spring", "春暖花开"),
SUMMER("summer", "夏日炎炎"),
AUTUMN("autumn", "秋高气爽"),
WINTER("winter", "白雪皑皑");
private final String seasonName;
private final String seasonDesc;
private Season (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 "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
public void show() {
System.out.println("这是一个季节");
}
}
public class TestSeason {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());
// 1. values() 方法:返回所有值
Season[] seasons = Season.values();
for (int i=0; i<seasons.length;i++) {
System.out.println(seasons[i]);
}
// 2. valueOf() 方法:返回指定对象的值(要求传入的形参name是枚举类对象的名字)
// 否则,报 java.lang.IllegalArgumentException 异常
String str = "SPRING";
Season sea = Season.valueOf(str);
System.out.println(sea);
}
}
可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同。(相当于让每个对象重写抽象方法)
改造如下:
interface Info {
void show();
}
enum Season implements Info {
SPRING("spring", "春暖花开"){
public void show() {
System.out.println("春天在哪里?");
}
},
SUMMER("summer", "夏日炎炎"){
public void show() {
System.out.println("夏天在哪里?");
}
},
AUTUMN("autumn", "秋高气爽"){
public void show() {
System.out.println("秋天在哪里?");
}
},
WINTER("winter", "白雪皑皑"){
public void show() {
System.out.println("冬天在哪里?");
}
};
private final String seasonName;
private final String seasonDesc;
private Season (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 "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]";
}
public void show() {
System.out.println("这是一个季节");
}
}
public class TestSeason {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());
// 1. values() 方法:返回所有值
Season[] seasons = Season.values();
for (int i=0; i<seasons.length;i++) {
System.out.println(seasons[i]);
}
// 2. valueOf() 方法:返回指定对象的值(要求传入的形参name是枚举类对象的名字)
// 否则,报 java.lang.IllegalArgumentException 异常
String str = "WINTER";
Season sea = Season.valueOf(str);
System.out.println(sea);
sea.show();
}
}
完。