枚举类
作者:互联网
1.什么是枚举
/* 一 . 枚举类的定义: 1.枚举类的理解 : 类的对象是有限的 , 确定的, 2.当需要定义一组常量时,强烈建议用枚举类 3,如果枚举类中只有一个对象, 则可以作为单例模式的实现方式 二. 定义枚举类 1.jdk5之前:自定义枚举类 2.jdk5之后:可以使用enum关键字定义枚举类 */
2.枚举类的使用
3.自定义枚举类
public class EnumTest1 { public static void main(String[] args) { Season spring = Season.SPRING; System.out.println(spring);//Season{seasonName='春天', seasonDesc='春暖花开'} } } //自定义枚举类 class Season{ //1.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //2.私有化构造器 private Season(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } //3.提供当前枚举类的多个对象 (类似于接口中的常量) 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("冬天","冰天雪地"); public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
4.使用enum关键字定义枚举类
/* 使用Enum 关键字定义枚举类 定义的枚举类默认继承于java.langEnum类 */ public class EnumTest2 { public static void main(String[] args) { Season1 season1 = Season1.SPRING; System.out.println(season1.toString()); System.out.println(season1.getClass().getSuperclass());//class java.lang.Enum } } //使用Enum 关键字定义枚举类 enum Season1{ //1.提供当前枚举类的多个对象 (类似于接口中的常量) : 多个对象之间用","隔开 SPRING("春天","春暖花开"), SUMMER("夏天","夏日炎炎"), AUTUMN("秋天","秋高气爽"), WINTER("冬天","冰天雪地"); //1.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //2.私有化构造器 private Season1(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season1{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
5.枚举类的主要方法
6.枚举类实现接口
public class EnumTest3 { public static void main(String[] args) { Season2 spring = Season2.SPRING; System.out.println(spring.toString()); System.out.println(spring.getClass().getSuperclass());//class java.lang.Enum System.out.println("***********************************************"); Season2[] values = Season2.values(); for (int i = 0; i < values.length; i++) { System.out.println(values[i]); values[i].show(); } } } interface info{ void show(); } enum Season2 implements info{ //枚举类实现接口 //1.重写一次show() /*@Override public void show() { }*/ //2.每个对象重写一个show() 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("我是冬天"); } }; //1.声明Season对象的属性:private final修饰 private final String seasonName; private final String seasonDesc; //2.私有化构造器 private Season2(String seasonName, String seasonDesc) { this.seasonName = seasonName; this.seasonDesc = seasonDesc; } public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season2{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
标签:String,Season,枚举,seasonName,seasonDesc,public 来源: https://www.cnblogs.com/Anonymity-zhang/p/14367914.html