Java基础系列(11)- 枚举类
枚举类的使用:入门
-
枚举类的理解:
类的对象只有有限个,确定的。我们称此类为枚举类
-
当需要定义一组常量时,强烈建议使用枚举类
-
如果枚举类中只有一个对象,则可以作为单例模式的实现方式
比如:星期、季节等
定义枚举类
自定义枚举类
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 seasonDese;
//2.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDese){
this.seasonName = seasonName;
this.seasonDese = seasonDese;
}
//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 getSeasonDese() {
return seasonDese;
}
//4.其他诉求1:提供toString()
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDese='" + seasonDese + '\'' +
'}';
}
}
使用enum关键字定义(JDK1.6)
说明:定义的枚举类默认继承于Java.lang.Enum
类
public class SeasonTest{
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring); //SPRING
System.out.println(Season.class.getSuperclass()); //class java.lang.Enum
}
}
enum Season {
//1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象 ";"结束
SPRING("春天", "春暖花开"),
SUMMER("夏天", "热死了"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冷死了");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDese;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDese) {
this.seasonName = seasonName;
this.seasonDese = seasonDese;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDese() {
return seasonDese;
}
}
Enum类的主要方法
values()方法
:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值
valueOf(String str)
:可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的"名字"。如不是,会有运行时异常:IllegalArgumentException
toString
:返回当前枚举类对象常量的名称
public class SeasonTest{
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring); //SPRING
//values()
Season[] values = Season.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]); //SPRING/SUMMER/AUTUMN/WINTER
}
//valueOf(String objName):
Season winter = Season.valueOf("WINTER");
System.out.println(winter); //WINTER
}
}
enum Season {
//1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象 ";"结束
SPRING("春天", "春暖花开"),
SUMMER("夏天", "热死了"),
AUTUMN("秋天", "秋高气爽"),
WINTER("冬天", "冷死了");
//2.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDese;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDese) {
this.seasonName = seasonName;
this.seasonDese = seasonDese;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDese() {
return seasonDese;
}
}
使用enum关键字定义的枚举类实现接口
情况一:实现接口,在enum类中实现抽象方法
public class SeasonTest{
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring); //SPRING
//values()
Season[] values = Season.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]); //SPRING/SUMMER/AUTUMN/WINTER
}
//valueOf(String objName):
Season winter = Season.valueOf("WINTER");
System.out.println(winter); //WINTER
winter.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 seasonDese;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDese) {
this.seasonName = seasonName;
this.seasonDese = seasonDese;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDese() {
return seasonDese;
}
@Override
public void show() {
System.out.println("这是一个季节!");
}
}
情况二:让枚举类的对象分别实现接口中的抽象方法
public class SeasonTest{
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring); //SPRING
//values()
Season[] values = Season.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]); //SPRING/SUMMER/AUTUMN/WINTER
}
//valueOf(String objName):
Season winter = Season.valueOf("WINTER");
System.out.println(winter); //WINTER
winter.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 seasonDese;
//3.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDese) {
this.seasonName = seasonName;
this.seasonDese = seasonDese;
}
//4.其他诉求:获取枚举类对象的属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDese() {
return seasonDese;
}
}