目录
一、枚举类
枚举类型本质上也是一种类,只不过是这个类的对象是有限的、固定的几个,不能让用户随意创建。
二、创建枚举类
2.1、JDK5.0之前创建
public class SeasonTest {
public static void main(String[] args) {
System.out.println(Season.SPRING.getSeasonName());
}
}
//JDK5.0之前创建方式
class Season{
//定义属性
private final String seasonName;
private final String seasonDesc;
//构造器私有化
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//提高get方法
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//创建当前类的实例
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("冬天","白雪皑皑");
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasonDesc='" + seasonDesc + '\'' +
'}';
}
}
2.2、JDK5.0使用enum创建
public class SeasonTest1 {
public static void main(String[] args) {
System.out.println(Season1.SPRING.getSeasonName());
}
}
//JDK5.0创建方式
enum Season1{
//创建当前类的实例
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
//定义属性
private final String seasonName;
private final String seasonDesc;
//构造器私有化
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//提高get方法
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
}
三、枚举类常用方法
String toString(): 默认返回的是常量名(对象名),可以继续手动重写该方法!
static 枚举类型[] values():返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值,是一个静态方法
static 枚举类型 valueOf(String name):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
String name():得到当前枚举常量的名称。建议优先使用toString()。
int ordinal():返回当前枚举常量的次序号,默认从0开始
//toString()
System.out.println(Season1.SPRING);
//name()
System.out.println(Season1.SPRING.name());
//values()
Season1[] values = Season1.values();
for (Season1 season1:values) {
System.out.println(season1);
}
//valueOf():可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。
// 如不是,会有运行时异常:IllegalArgumentException。
System.out.println(Season1.valueOf("SPRING"));
//ordinal()
System.out.println(Season1.SUMMER.ordinal());
四、枚举类实现接口
4.1、枚举的对象调用执行同一个方法
public class SeasonTest2 {
public static void main(String[] args) {
Season2.SPRING.method();
}
}
interface Test01{
public void method();
}
//JDK5.0创建方式
enum Season2 implements Test01{
//创建当前类的实例
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
@Override
public void method() {
System.out.println("实现接口成功");
}
//定义属性
private final String seasonName;
private final String seasonDesc;
//构造器私有化
private Season2(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
}
4.2、枚举类的每个对象重写接口中的方法,执行每个对象重写的方法
public class SeasonTest2 {
public static void main(String[] args) {
Season2[] values = Season2.values();
for (int i = 0; i < values.length; i++) {
values[i].method();
}
}
}
interface Test01{
public void method();
}
//JDK5.0创建方式
enum Season2 implements Test01{
//创建当前类的实例
SPRING("春天","春暖花开"){
@Override
public void method() {
System.out.println("春天重写接口方法");
}
},
SUMMER("夏天","夏日炎炎"){
@Override
public void method() {
System.out.println("夏天重写接口方法");
}
},
AUTUMN("秋天","秋高气爽"){
@Override
public void method() {
System.out.println("秋天重写接口方法");
}
},
WINTER("冬天","白雪皑皑"){
@Override
public void method() {
System.out.println("冬天重写接口方法");
}
};
// @Override
// public void method() {
// System.out.println("实现接口成功");
// }
//定义属性
private final String seasonName;
private final String seasonDesc;
//构造器私有化
private Season2(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
}