枚举
什么是枚举?
所谓的枚举,就是把某个类所有的对象一一罗列出来。
枚举类:这个类的对象是有限的。
定义枚举的语法格式
public enum 枚举类类名{
值1, 值2, 值3, 值4;
}
示例
public enum Season {
SPRING, SUMMER, AUTUMN, WINTER;
}
枚举的使用
枚举一般是配合switch…case使用的。
public class TestEnum {
public static void main(String[] args) {
Season s = Season.SPRING;
switch(s) {
case SPRING:{
System.out.println("春天想去小河边散步");
break;
}
case SUMMER:{
System.out.println("夏天我想逆流而上");
break;
}
case AUTUMN:{
System.out.println("秋天想去拍婚纱照");
break;
}
case WINTER:{
System.out.println("冬天去三亚玩");
break;
}
}
}
}
枚举的剖析
枚举的本质是一个类,这个类继承于Enum。
public final class Season extends Enum
{
public static final Season SPRING;
public static final Season SUMMER;
public static final Season AUTUMN;
public static final Season WINTER;
private static final Season ENUM$VALUES[];
private Season(String s, int i)
{
super(s, i);
}
public static Season[] values()
{
Season aseason[];
int i;
Season aseason1[];
System.arraycopy(aseason = ENUM$VALUES, 0, aseason1 = new Season[i = aseason.length], 0, i);
return aseason1;
}
public static Season valueOf(String s)
{
return (Season)Enum.valueOf(com/lanu/enumeration/Season, s);
}
static
{
SPRING = new Season("SPRING", 0);
SUMMER = new Season("SUMMER", 1);
AUTUMN = new Season("AUTUMN", 2);
WINTER = new Season("WINTER", 3);
ENUM$VALUES = (new Season[] {
SPRING, SUMMER, AUTUMN, WINTER
});
}
}
上述代码是对Season反编译的结果,从反编译的结果可以看出:枚举本质上是一个类。每一个枚举值本质上是一个(public static final)对象。
既然是对象,就可以调用方法,方法要么是自己定义的,要么是继承于父类的方法。
它的父类提供了2个常用的方法:
String name(); //获取枚举值的名字
int ordinal(); //获取枚举值的序号,序号从0开始。
由于枚举本质上是一个类,所以枚举可以添加属性,添加方法,添加构造器等,类所能做的事情,它都可以做。
下面为Season添加属性,方法以及构造器:
public enum Season {
SPRING("春"), SUMMER("夏"), AUTUMN("秋"), WINTER("冬");
private String name;//自定义属性
private Season(String name) {//自定义构造器
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void test() {//自定义方法
System.out.println("hello,my name is " + this.name());
}
}
反编译后的结果如下:
import java.io.PrintStream;
public final class Season extends Enum
{
public static final Season SPRING;
public static final Season SUMMER;
public static final Season AUTUMN;
public static final Season WINTER;
private String name;
private static final Season ENUM$VALUES[];
private Season(String s, int i, String name)
{
super(s, i);
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void test()
{
System.out.println((new StringBuilder("hello,my name is")).append(name()).toString());
}
public static Season[] values()
{
Season aseason[];
int i;
Season aseason1[];
System.arraycopy(aseason = ENUM$VALUES, 0, aseason1 = new Season[i = aseason.length], 0, i);
return aseason1;
}
public static Season valueOf(String s)
{
return (Season)Enum.valueOf(com/lanu/enumeration/Season, s);
}
static
{
SPRING = new Season("SPRING", 0, "春");
SUMMER = new Season("SUMMER", 1, "夏");
AUTUMN = new Season("AUTUMN", 2, "秋");
WINTER = new Season("WINTER", 3, "冬");
ENUM$VALUES = (new Season[] {
SPRING, SUMMER, AUTUMN, WINTER
});
}
}
枚举的总结
枚举就是一种数据类型,只不过这种数据类型把可能出现的值提前定义好了,用的时候拿类直接访问即可。枚举多用于switch…case和if语句。