JAVA_基础枚举类
枚举类的理解
1)类的对象只有有限个,确定的。
2)当需要定义一组常量时,强烈建议使用枚举类。
3)如果枚举类中只有一个对象,则可以作为单例模式的实现方式。
如何定义枚举类
方式一:JDK5.0之前,自定义枚举类。
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("冬天","白雪皑皑");
//4.其它诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//5.其它诉求:提供toString方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
方式二:JDK5.0,可以使用enum
关键字定义枚举类。
1)定义的枚举类默认继承于java.lang.Enum
类。
2)可以省去public final static Season
= new Season
。
public class SeasonenumTest {
public static void main(String[] args) {
Season1 season1 = Season1.AUTUMN;
System.out.println(season1);
System.out.println(Season1.class.getSuperclass());
}
}
enum Season1 {
//1.提供当前枚举类的多个对象,多个对象之间用逗号隔开,末尾用分号结束。
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
//2.声明Season对象的属性:private final修饰
private final String seasonDesc;
private final String seasonName;
//3.私有化类的构造器,并给对象赋值
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其它诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
Enum类的主要方法
values()
:返回枚举类型的对象数组。该方法可以很方便地遍历所有的 枚举值。
valueOf(String str)
:可以把一个字符串转为对应的枚举类对象。要求字符 串必须是枚举类对象的“名字”。如不是,会有运行时异常: IllegalArgumentException
。
toString()
:返回当前枚举类对象常量的名称。
public class SeasonenumTest {
public static void main(String[] args) {
Season1 season1 = Season1.AUTUMN;
//toString()
System.out.println(season1.toString());
System.out.println(Season1.class.getSuperclass());
System.out.println("***********************");
//values()
Season1[] values = Season1.values();
for(int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
System.out.println("***********************");
Thread.State[] values1 = Thread.State.values();
for (int i = 0; i < values1.length; i++) {
System.out.println(values1[i]);
}
//valuesOf(String objName):返回枚举类中对象名是objName的对象。
//如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
Season1 winter = Season1.valueOf("WINTER");
System.out.println(winter);
}
}
enum Season1 {
//1.提供当前枚举类的多个对象,多个对象之间用逗号隔开,末尾用分号结束。
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
//2.声明Season对象的属性:private final修饰
private final String seasonDesc;
private final String seasonName;
//3.私有化类的构造器,并给对象赋值
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//4.其它诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
使用enum关键字定义的枚举类实现接口的情况
1)情况一:实现接口,在enum
类中实现抽象方法。
2)情况二:让枚举的对象分别实现接口中的抽象方法。