1.枚举的作用:枚举的最大作用就是让某种类型变量的取值只能为若干固定值中的一个,否则编译器将会报错。下面通过一个普通类来实现枚举的功能,理解枚举类型的实现原理:
public class Season { public final static Season SPRING = new Season(); public final static Season SUMMER = new Season(); public final static Season AUTUMN = new Season(); public final static Season WINTER = new Season(); /* * 将构造方法写为private形式,是不让外部对Season类进行实例化.*/ private Season () { } public Season nextSeason () { Season season = null; if (this == SPRING) { season = SUMMER; }else if (this == SUMMER) { season = AUTUMN; }else if (this == AUTUMN) { season = WINTER; }else { season = SPRING; } return season; } public String toString () { String str = null; if (this == SPRING) { str = "春天"; }else if (this == SUMMER) { str = "夏天"; }else if (this == AUTUMN) { str = "秋天"; }else { str = "冬天"; } return str; } /* * 得到一个由所有季节组成的数组 */ public static Season[] values () { Season[] seasons = new Season[4]; Season season = Season.SPRING; for (int i = 0;i < seasons.length;i++) { seasons[i] = season; season = season.nextSeason(); } return seasons; } }
/* * 一个测试类 */ public class TestSeason { public static void main (String[] args) { for (Season season : Season.values()) { System.out.println("当前季节为:"+season.toString()); System.out.println("下一个季节为:"+season.nextSeason()); System.out.println("-----------------------------------"); } } }
运行结果为:
2.枚举的简单应用:
public enum Season { SPRING,SUMMER,AUTUMN,WINTER }
shu
public class TestSeason { public static void main (String[] args) { Season season = Season.SPRING; System.out.println("这个季节是:"+season); System.out.println("这个季节是:"+season.name());//season.name()输出season在Season中的枚举常量. System.out.println("这个季节是:"+season.ordinal());//season.ordinal()输出season在Season中的序数. System.out.println("这个季节是:"+Season.valueOf("SPRING"));//Season.valueOf("SPRING")返回字符串中所对应的对象。 Season[] seasons = Season.values();//Season.values()返回Season中的所有对象。 for (Season season : seasons) { System.out.println(season); } } }
输出结果为:
这个季节是:SPRING 这个季节是:SPRING 这个季节是:0 这个季节是:SPRING SPRING SUMMER AUTUMN WINTER
3.枚举的高级特性:
(1)带有构造函数的Enum:Enum和类一样,也有构造函数。代码如下:
public enum EnumCons { SPRING("春天"),SUMMER,AUTUMN(),WINTER; private EnumCons () { System.out.println("不带参数的构造函数"); } private EnumCons (String string) { System.out.println(string+":带参数的构造函数"); } }
public class TestEnumCons { public static void main (String[] args) { EnumCons enumCons = EnumCons.AUTUMN; } }
运行结果如下:
春天:带参数的构造函数 不带参数的构造函数 不带参数的构造函数 不带参数的构造函数
(2)带有抽象方法的Enum:
public enum EnumAbstract { SPRING { public EnumAbstract next () { return SUMMER; } public String toString () { return "春天"; } },SUMMER{ public EnumAbstract next () { return AUTUMN; } public String toString () { return "夏天"; } },AUTUMN{ public EnumAbstract next () { return WINTER; } public String toString () { return "秋天"; } },WINTER{ public EnumAbstract next () { return SPRING; } public String toString () { return "冬天"; } }; public abstract EnumAbstract next (); public abstract String toString (); }
public class TestEnumAbstract { public static void main (String[] args) { EnumAbstract[] enumAbstracts = EnumAbstract.values(); for (EnumAbstract enumAbstract : enumAbstracts) { System.out.println("当前季节为:"+enumAbstract); System.out.println("下一个季节为:"+enumAbstract.next()); System.out.println("------------------------------------"); } } }
运算结果为:
当前季节为:春天 下一个季节为:夏天 ------------------------------------ 当前季节为:夏天 下一个季节为:秋天 ------------------------------------ 当前季节为:秋天 下一个季节为:冬天 ------------------------------------ 当前季节为:冬天 下一个季节为:春天 ------------------------------------