一、理解与使用:
- 枚举类的理解:类的对象只有有限个,确定的,我们称此类为枚举类。
- 当我们定义一组常量时,强烈建议使用枚举类。
- 枚举属于一种特殊的类,里面只包含一组有限的特定的对象
- 如果枚举类中只有一个对象时,则可以作为单例模式的实现方式
二、如何定义枚举类
方式一:JDK5.0之前,自定义枚举类
package www; public class SeasonTest { //测试 public static void main(String[] args) { System.out.println(Season.SUMMER); } } //自定义枚举类 class Season{ //1.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //2.私有化类的构造器,防止直接new,并给对象属性赋值 private Season(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //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.其他诉求1:获取枚举类对象的属性 public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } //5.其他诉求2:提供toString()方法 @Override public String toString() { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; /*return seasonName;*/ } }
输出结果:
自定义枚举类总结如下:
方法二:JDK5.0之后,可以使用enum定义枚举类
package www; public class SeasonTest1 { public static void main(String[] args) { System.out.println(Season1.WINTER); //查Season1继承的父类:class java.lang.Enum System.out.println(Season1.class.getSuperclass()); } } //使用enum自定义枚举类 enum Season1{ //1.常量名(实参列表) //2.提供当前枚举类的多个对象,多个对象之间用","隔开,末尾用";"结束 //3.如果使用enum来实现枚举,要求将定义常量对象,写在前面 //4.如果我们使用的是无参构造器,创建常量对象,则可以省略() SPRING("春天","春暖花开"), SUMMER("夏天","夏季炎炎"), AUTUMN("秋天","秋高气爽"), WINTER("冬天","冰天雪地"),What; //2.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //无参构造器 private Season1(){} //3.私有化类的构造器,并给对象属性赋值 private Season1(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //4.其他诉求1:获取枚举类对象的属性 public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } }
输出结果:
使用关键字实现枚举类的注意事项
应用实例
public class EnumerationExercise01 { public static void main(String[] args) { Gender2 boy = Gender2.BOY; Gender2 boy1 = Gender2.BOY; System.out.println(boy);//输出BOY //本质就是调用Cender2的父类的toString 方法 System.out.println(boy == boy1); } } enum Gender2 { BOY, GIRL; }
输出结果如下:
BOY true
三、Enum类中常用的方法:
- values()方法:返回枚举类型的对象数组,该方法可以很方便的遍历所有的枚举值
- valuesOf(String str):可以把一个字符串转化为对应的枚举类对象,要求字符串必须是枚举类对象。
- 返回当枚举类对象的常量名称。
System.out.println("*******************"); //1.toString() System.out.println(Season1.WINTER.toString()); System.out.println("*******************"); //2.values() Season1[] values = Season1.values(); for (int i = 0; i < values.length; i++) { System.out.println(values[i]); } System.out.println("*******************"); //3.valuesOf():返回枚举类中对象名是objName的对象 //如果没有objName的枚举类对象,则抛异常IllegalArgumentException // Season1 season1 = Season1.valueOf("WINTER1"); Season1 season1 = Season1.valueOf("WINTER"); System.out.println(season1);
输出结果:
public class EnumMethod { public static void main(String[] args) { //使用Season2枚举类,来演示各种方法 Season2 autumn = Season2.AUTUMN; //输出枚举对象的方法 System.out.println(autumn.name()); //ordinal() 输出的是该枚举对象的次序/编号,从0开始编号 //AUTUMN枚举对象是第四个,因此输出3 System.out.println(autumn.ordinal()); //从反编译可以看出values方法,返回Season2[] //含有定义的所有枚举对象 Season2[] values = Season2.values(); System.out.println("==遍历取出枚举对象(增强for)=="); for (Season2 value : values) { System.out.println(value); } //valueOf:将字符串转换成枚举对象,要求字符串必须为已有的常量名,否则报异常 //执行流程 //1.根据你输入的"AUTUMN" 到Season2的枚举对象去查找 //2.如果找到了,就返回,如果没有找到,就报错 Season2 autumn1 = Season2.valueOf("AUTUMN"); System.out.println("autumn1=" + autumn1); System.out.println(autumn == autumn1); //true //compareTo:比较两个枚举常量,比较的就是编号 /* public final int compareTo(E o) { self.ordinal - other.ordinal } Season2.AUTUMN编号[3] - Season2.SUMMER编号[2] */ System.out.println(Season2.AUTUMN.compareTo(Season2.SUMMER)); } } enum Season2 {//类 SPRING("春天", "温暖"), WINTER("冬天", "寒冷"), SUMMER("夏天", "炎热"), AUTUMN("秋天", "凉爽"); private String name; private String desc; //描述 private Season2(String name, String desc) { this.name = name; this.desc = desc; } public String getName() { return name; } public String getDesc() { return desc; } @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
输出结果如下:
AUTUMN 3 ==遍历取出枚举对象(增强for)== Season{name='春天', desc='温暖'} Season{name='冬天', desc='寒冷'} Season{name='夏天', desc='炎热'} Season{name='秋天', desc='凉爽'} autumn1=Season{name='秋天', desc='凉爽'} true 1
四、使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法
情况一就是简单的实现接口,这里就不演示代码了。
情况二:让枚举类的对象,分别实现接口中的抽象方法。
interface info{ void show(); } //使用enum自定义枚举类 enum Season1 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("冬天"); } };