文章目录
一、枚举类的使用
- 枚举类的理解:类的对象只有有限个,确定的。我们称此为枚举类
- 当需要定义一组常量时,强烈建议使用枚举类
- 如果枚举类中只有一个对象,则可以作为单例模式的实现方式
二 、如何定义枚举类
方式一:JDK5.0之前,自定义枚举类
方式二:JDK5.0,可以使用enum关键字定义枚举类
2.1 自定义枚举类
步骤:
- 声明Season对象的属性:private final修饰
- 私有化类的构造器,并给对象属性赋值
- 提供当前枚举类的多个对象:public static final的
public class SeasonTest {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
}
}
class Season{
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
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("冬天","冰天雪地");
//其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//其他诉求2:提供toString方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
输出结果:
2.2 使用关键字enum定义枚举类
步骤:
- 提供当前枚举类的多个对象:多个对象之间用逗号,隔开,末尾对象用分号;
- 声明Season对象的属性:private final修饰
- 私有化类的构造器,并给对象属性赋值
public class SeasonTest{
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
System.out.println(autumn);
System.out.println(autumn.getSeasonName());
System.out.println(autumn.getSeasonDesc());
System.out.println(Season.class.getSuperclass());
}
}
enum Season {
//1.提供当前枚举类的多个对象:多个对象之间用逗号,隔开,末尾对象用分号;
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
运行结果为:
三、Enum类的主要方法
Enum类为enum的父类
- toString():返回当前枚举类对象常量的名称
- values():返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值
- valueOf(String str):返回枚举类中对象名为objName的对象
package Season;
public class SeasonTest{
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
//values()
Season[] value = Season.values();
for (int i = 0; i < value.length; i++) {
System.out.println(value[i]);
}
//valueOf(String objName)
System.out.println(Season.valueOf("WINTER"));
}
}
enum Season {
//1.提供当前枚举类的多个对象:多个对象之间用逗号,隔开,末尾对象用分号;
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
输出结果为:
四、使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法
public class SeasonTest{
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
autumn.show();
}
}
interface Info{
void show();
}
enum Season implements Info{
//1.提供当前枚举类的多个对象:多个对象之间用逗号,隔开,末尾对象用分号;
SPRING("春天", "春暖花开"),
SUMMER("夏天", "夏日炎炎"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冰天雪地");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public void show() {
System.out.println("这是一个季节");
}
}
输出结果:
情况二:让枚举类的对象分别去实现接口中的抽象方法
上述这种情况是,每一个枚举类的对象调用show方法,都会打印“这是一个季节”这句话,现在我们想要做出改进,想要在每一个对象调用show方法,展现出来的是不同的内容,我们可以让每一个对象去重写一下show方法
public class SeasonTest{
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
autumn.show();
}
}
interface Info{
void show();
}
enum Season 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("这是冬天");
}
};
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//其他诉求1:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
输出结果: