这篇文章主要介绍“Java枚举enum如何实现”,在日常操作中,相信很多人在Java枚举enum如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java枚举enum如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一.枚举介绍
枚举是一组常量的集合
理解:美剧属于一种特殊的类,里面只包含一组有限的特定的对象
1.自定义枚举实现
不需要提供Set方法,枚举对象值通常为只读。
对枚举对象/属性使用final和static关键字修饰,实现底层优化
枚举对象名通常使用全部大写,常量的命名规范
枚举·对象根据需要,可以有一到多个属性
(1)示例
package com.demo;import javax.sound.midi.Soundbank;public class Enum01 { public static void main(String[] args) { System.out.println(Season.SPRING); System.out.println(Season.SUMMER); System.out.println(Season.AUTUMN); System.out.println(Season.WINTER); }}//演示自定义枚举实现class Season{ private String name; private String desc;//描述特性 //1.将构造器私有化,目的防止直接new出来 private Season(String name, String desc) { this.name = name; this.desc = desc; } //2.去掉set方法,只能读取,不能修改属性 public String getName() { return name; } public String getDesc() { return desc; } //3.在season内部创建固定的对象 //定义了四个对象// public static Season SPRING=new Season("春天","温暖");// public static Season SUMMER=new Season("夏天","炎热");// public static Season AUTUMN=new Season("秋天","凉爽");// public static Season WINTER=new Season("冬天","寒冷"); //最终优化,加入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("冬天","寒冷"); //重写toString方法 @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; }}
(2)测试结果
2.enum关键字实现枚举
如果使用enum关键字来实现枚举类
使用关键字enum替换class
public static final Season02 SPRING=new Season02(“春天”,“温暖”);可替换为 SPRING(“春天”,“温暖”),//解读:常量名(实参列表)
如果有多个常量使用逗号间隔即可
如果使用enum关键字实现枚举,要求将定义的常量对象写下前面
(1)示例
package com.demo;public class Enum02 { public static void main(String[] args) { System.out.println(Season.SPRING); System.out.println(Season.SUMMER); System.out.println(Season.AUTUMN); System.out.println(Season.WINTER); }}//演示使用enum关键字来实现枚举类enum Season02{ //如果使用enum关键字来实现枚举类 //1.使用关键字enum替换class //2.public static final Season02 SPRING=new Season02("春天","温暖");可替换为 SPRING("春天","温暖"),//解读:常量名(实参列表) //3.如果有多个常量使用逗号间隔即可 SUMMER("夏天","炎热"), AUTUMN("秋天","凉爽"), WINTER("冬天","寒冷"); //4.如果使用enum关键字实现枚举,要求将定义的常量对象写下前面 private String name; private String desc;//描述特性 //1.将构造器私有化,目的防止直接new出来 private Season02(String name, String desc) { this.name = name; this.desc = desc; } //2.去掉set方法,只能读取,不能修改属性 public String getName() { return name; } public String getDesc() { return desc; } //3.在season内部创建固定的对象 //定义了四个对象// public static Season SPRING=new Season("春天","温暖");// public static Season SUMMER=new Season("夏天","炎热");// public static Season AUTUMN=new Season("秋天","凉爽");// public static Season WINTER=new Season("冬天","寒冷"); //最终优化,加入final// public static final Season02 SPRING=new Season02("春天","温暖");// public static final Season02 SUMMER=new Season02("夏天","炎热");// public static final Season02 AUTUMN=new Season02("秋天","凉爽");// public static final Season02 WINTER=new Season02("冬天","寒冷"); //重写toString方法 @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; }}
(2)测试结果
3.enum关键字注意事项
当我i们使用enum关键字开发一个枚举类时,默认会继承Enum类
传统的public static final Season02 SPRING=new Season02("春天","温暖");
,这里要知道他调用的是哪个构造器
如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略.
当有多个枚举对象时,使用逗号间隔,最后有一个分号结尾
枚举对象必须放在枚举类的行首
(1)示例
package com.demo;public class Enum02 { public static void main(String[] args) { System.out.println(Season.SPRING); System.out.println(Season.SUMMER); System.out.println(Season.AUTUMN); System.out.println(Season.WINTER); }}//演示使用enum关键字来实现枚举类enum Season02{ //如果使用enum关键字来实现枚举类 //1.使用关键字enum替换class //2.public static final Season02 SPRING=new Season02("春天","温暖");可替换为 SPRING("春天","温暖"),//解读:常量名(实参列表) //3.如果有多个常量使用逗号间隔即可 SUMMER("夏天","炎热"), AUTUMN("秋天","凉爽"), WINTER("冬天","寒冷"), //3.如果使用无参构造器创建枚举对象,则实参列表和小括号都可以省略.两种写法 HAHA,//调用无参构造器 HAHAHA();//调用无参构造器 //4.如果使用enum关键字实现枚举,要求将定义的常量对象写下前面 private String name; private String desc;//描述特性 //1.将构造器私有化,目的防止直接new出来 private Season02(String name, String desc) { this.name = name; this.desc = desc; } private Season02() { //无参构造器 } //2.去掉set方法,只能读取,不能修改属性 public String getName() { return name; } public String getDesc() { return desc; } //3.在season内部创建固定的对象 //定义了四个对象// public static Season SPRING=new Season("春天","温暖");// public static Season SUMMER=new Season("夏天","炎热");// public static Season AUTUMN=new Season("秋天","凉爽");// public static Season WINTER=new Season("冬天","寒冷"); //最终优化,加入final// public static final Season02 SPRING=new Season02("春天","温暖");// public static final Season02 SUMMER=new Season02("夏天","炎热");// public static final Season02 AUTUMN=new Season02("秋天","凉爽");// public static final Season02 WINTER=new Season02("冬天","寒冷"); //重写toString方法 @Override public String toString() { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; }}
到此,关于“Java枚举enum如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!