枚举
1、理解
枚举(Enumeration,通常简写为enum)在编程语言中是一种特殊的数据类型,它允许你为变量定义一组命名的常量。这些常量通常表示一组有限的值,比如一周的七天、颜色的集合或者状态的集合等。枚举类型的主要目的是提高代码的可读性和可维护性,同时减少错误的可能性。
2、定义
枚举类型是通过关键字
enum
定义的,后面跟着枚举类型的名称和用逗号分隔的枚举值列表。每个枚举值都是该类型的一个实例,并且每个枚举值都是唯一的。
3、特点
- 枚举类型的值是预定义的,并且是有限的。
- 枚举类型的值是常量,一旦定义后就不能修改。
- 枚举类型的值具有类型安全性,即它们是强类型的,不能与其他类型混淆。
- 枚举类型可以定义属性和方法,就像普通的类一样,但枚举类型的构造器通常是私有的,以防止从外部创建枚举实例。
4、使用
1、案例一季节类
package com.xx.enum02;public class Test01 {/*** 知识点:枚举的引入* * 需求:编写季节类(Season),该类只有四个对象(spring,summer,autumn,winter)*/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);}
}
package com.xx.enum02;//季节枚举类
//注意:枚举就是一个受限制的类(该类只能有固定的几个对象,不能让外界创建对象)
//注意:枚举不能有继承关系(显示继承)
//注意:自定义枚举类隐式继承Enum类 -- public class Season extends Enum{}
public enum Season{//注意:枚举类的第一行必须声明对象//spring;//底层实现 - public static final Season spring = new Season();//spring();//底层实现 - public static final Season spring = new Season();spring("春天","春雨绵绵"),//底层实现 - public static final Season spring = new Season("春天","春雨绵绵");summer("夏天","烈日炎炎"),//底层实现 - public static final Season spring = new Season("夏天","烈日炎炎");autumn("秋天","硕果累 累"),//底层实现 - public static final Season spring = new Season("秋天","硕果累累");winter("冬天","白雪皑皑");//底层实现 - public static final Season spring = new Season("冬天","白雪皑皑");private String name;private String info;//注意:枚举类的构造方法使用private修饰private Season() {}private Season(String name, String info) {this.name = name;this.info = info;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}@Overridepublic String toString() {return name + " -- " + info;}
}
2、案例二信号机
package com.xx.enum06;//信号灯
public enum Signal {RED, YELLOW, GREEN;
}
package com.xx.enum06;import java.util.Scanner;public class Test01 {/*** 知识点:枚举案例 之 状态机*/public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请选择信号灯:RED, YELLOW, GREEN");String str = scan.next();Signal signal = Signal.valueOf(str);String trafficInstruct = getTrafficInstruct(signal);System.out.println(trafficInstruct);scan.close();}public static String getTrafficInstruct(Signal signal) {String trafficInstruct = "信号灯故障";switch (signal) {case RED:trafficInstruct = "红灯停";break;case YELLOW:trafficInstruct = "黄灯请注意";break;case GREEN:trafficInstruct = "绿灯行";break;}return trafficInstruct;}
}
3、案例三错误码
package com.xx.enum07;public enum AddCode {ERR01(-1,"添加失败 - 学生信息不合法"),ERR02(-2,"添加失败 - 没有该学生"),OK(1,"添加成功");private int code;private String message;private AddCode(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic String toString() {return code + " -- " + message;}
}
package com.xx.enum07;public class Test01 {/*** 知识点:枚举案例 之 错误码*/public static void main(String[] args) {System.out.println(AddCode.ERR01);System.out.println(AddCode.ERR02);System.out.println(AddCode.OK);}
}
4、案例四 组织枚举
package com.qf.enum08;public interface Code {enum AddCode {ERR01(-1,"添加失败 - 学生信息不合法"),ERR02(-2,"添加失败 - 有重复学生"),OK(1,"添加成功");private int code;private String message;private AddCode(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic String toString() {return code + " -- " + message;}}enum RemoveCode {ERR01(-1,"删除失败 - 学生信息不合法"),ERR02(-2,"删除失败 - 没有该学生"),OK(1,"删除成功");private int code;private String message;private RemoveCode(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic String toString() {return code + " -- " + message;}}enum UpdateCode {ERR01(-1,"修改失败 - 学生信息不合法"),ERR02(-2,"修改失败 - 没有该学生"),ERR03(-3,"修改失败 - 修改数据不合法"),ERR04(-4,"修改失败 - 目标班级上有学生"),ERR05(-5,"修改失败 - 目标学号上有学生"),OK(1,"修改成功");private int code;private String message;private UpdateCode(int code, String message) {this.code = code;this.message = message;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}@Overridepublic String toString() {return code + " -- " + message;}}}
package com.qf.enum08;public class Test01 {/*** 知识点:枚举案例 之 组织枚举* * 应用场景:可以将一个一个的错误码使用类或接口组织在一起* * 注意:* 使用类组织枚举,枚举默认使用static修饰* 使用接口组织枚举,枚举默认使用public static修饰* 经验:使用接口组织众多枚举*/public static void main(String[] args) {System.out.println(Code.AddCode.ERR01);}
}