- 1、枚举类
- 2、自定义枚举类
- 3、使用enum关键字定义枚举类
- 4、Enum类的常用方法
- 5、枚举类实现接口
- 6、应用举例
枚举类的对象是有限个,确定的。通常用来定义一组常量,例如:季节、星期、性别等。
2、自定义枚举类
JDK1.5之前均需要自定义枚举类。如下,定义了一个枚举类:季节。
public class Season {
//属性
private final String seasonName;
private final String seasonMonth;
//利用私有化构造器进行赋值
private Season(String seasonName, String seasonMonth) {
this.seasonName = seasonName;
this.seasonMonth = seasonMonth;
}
//提供枚举类对象
public static final Season SPRING = new Season("春天", "1-3");
public static final Season SUMMER = new Season("夏天", "4-6");
public static final Season AUTUMN = new Season("秋天", "7-9");
public static final Season WINTER = new Season("冬天", "10-12");
//get()方法
public String getSeasonName() {
return seasonName;
}
public String getSeasonMonth() {
return seasonMonth;
}
//toString()方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + ''' +
", seasonMonth='" + seasonMonth + ''' +
'}';
}
}
3、使用enum关键字定义枚举类
JDK1.5之后,使用enum关键字创建枚举类。
public enum Season {
//提供枚举类对象,每个对象用逗号连接
SPRING("春天", "1-3"),
SUMMER("夏天", "4-6"),
AUTUMN("秋天", "7-9"),
WINTER("冬天", "10-12");
//属性
private final String seasonName;
private final String seasonMonth;
//利用私有化构造器进行赋值
private Season(String seasonName, String seasonMonth) {
this.seasonName = seasonName;
this.seasonMonth = seasonMonth;
}
//get()方法
public String getSeasonName() {
return seasonName;
}
public String getSeasonMonth() {
return seasonMonth;
}
//toString()方法
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + ''' +
", seasonMonth='" + seasonMonth + ''' +
'}';
}
}
- 使用Season.class.getSuperclass().getName() 获取该枚举类对应的父类名字,得到java.lang.Enum。说明enum关键字对应的上层父类为java.lang.Enum;而自定义的枚举类的上层父类为Object。
4、Enum类的常用方法
(1)values()方法:将枚举类的所有对象以数组形式返回,利用此方法可以方便地遍历枚举类的值;
(2)valueOf(String name)方法:返回枚举类中对象名为name的枚举对象。若没有,则抛出IllegalArgumentException的异常;
(3)toString()方法:返回枚举类对象的名称。
Season[] values = Season.values();
for(Season s : values)
System.out.println(s);
Season spring = Season.valueOf("SPRING");
System.out.println(spring);
5、枚举类实现接口
(1)若枚举类实现一个接口,并重写该接口的方法,则该枚举类中所有枚举对象调用该方法时均相同;
public enum Season implements TestInterface{
SPRING,
SUMMER,
AUTUMN,
WINTER;
@Override
public void show() {
System.out.println("show...");
}
}
(2)若希望不同枚举对象对该接口的实现方法不同,则每一个枚举对象都需要重写该方法。
public enum Season implements TestInterface{
SPRING{
@Override
public void show() {
System.out.println("Spring is coming!");
}
},
SUMMER{
@Override
public void show() {
System.out.println("Summer is coming!");
}
},
AUTUMN{
@Override
public void show() {
System.out.println("Autumn is coming!");
}
},
WINTER{
@Override
public void show() {
System.out.println("Winter is coming!");
}
};
}
6、应用举例
(1)例如定义一个Person类,包含name,age,sex等属性。其中sex要求只能为male或者female,此时就可以使用枚举类进行限制。
代码示例:
public enum Sex {
male,
female;
}
public class Person {
private Sex sex;
public Sex getSex() {
return sex;
}
public void setSex(Sex sex) {
this.sex = sex;
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.setSex(Sex.male);//只能传入Sex的对象
}
}
(2)枚举结合switch使用
注:
1、case中只能使用枚举类对象名,不能加类名;
2、switch后面的括号中可以为:int、short、byte、char、String、枚举。
Person p = new Person();
p.setSex(Sex.male);
switch (p.getSex()){
case male:
System.out.println("a male");
break;
case female:
System.out.println("a female");
break;
}



