自定义枚举
一、为什么要使用枚举
在没有枚举之前,如果要表示多种类型,我们习惯使用常量的方式来定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // 定义季节常量类 public class Season { public static final int SPRING = 1 ; public static final int SUMMER = 2 ; public static final int AUTUMN = 3 ; public static final int WINTER = 4 ; } // 季节服务类 public class SeasonService { public void getSeasonInfo( int type) { switch (type) { case 1 : System.out.println( "春天" ); break ; case 2 : System.out.println( "夏天" ); break ; case 3 : System.out.println( "秋天" ); break ; case 4 : System.out.println( "冬天" ); break ; default : System.out.println( "不止今夕是何年" ); } } } // 测试类 public class Client { public static void main(String[] args) { SeasonService seasonService = new SeasonService(); seasonService.getSeasonInfo( 1 ); } } |
上面的示例看上去并没有啥问题,主要是在使用的地方没有明确性的约束 ,我们的本意是想根据 1,2,3,4 获取不同的季节信息,但是 seasonService.getSeasonInfo(1) 这里的参数可以是 int 类型的任意值,传入了不符合预期的标识编译不会报错
有了枚举之后上面这种情况就可以通过枚举进行严格的约束,并且枚举更加见名知意
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | // 季节枚举类 public enum Season { SPRING( 1 ), SUMMER( 2 ), AUTUMN( 3 ), WINTER( 4 ); private Integer type; Season(Integer type) { this .type = type; } public Integer getType() { return type; } } // 季节服务类 public class SeasonService { public void getSeasonInfo(Season season) { switch (season.getType()) { case 1 : System.out.println( "春天" ); break ; case 2 : System.out.println( "夏天" ); break ; case 3 : System.out.println( "秋天" ); break ; case 4 : System.out.println( "冬天" ); break ; } } } // 测试类 public class Client { public static void main(String[] args) { SeasonService seasonService = new SeasonService(); // 参数只能传入 Season.SPRING、Season.SUMMER、Season.SPRING、Season.WINTER // 传入其它类型编译检查报错,并且 Season.SPRING 相比于 1 来说更加见名知意 seasonService.getSeasonInfo(Season.SPRING); } } |
二、自定义枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // 季节枚举类 public enum Season { SPRING( 1 , "春天" ), SUMMER( 2 , "夏天" ), AUTUMN( 3 , "秋天" ), WINTER( 4 , "冬天" ); private Integer type; private String message; Season(Integer type, String message) { this .type = type; this .message = message; } public Integer getType() { return type; } public String getMessage() { return message; } public static String getMessageByType(Season season){ String message = "" ; for (Season value : Season.values()) { if (Objects.equals(season.type,value.type)){ message = value.message; break ; } } return message; } } // 测试类 public class Client { public static void main(String[] args) { String message = Season.getMessageByType(Season.AUTUMN); System.out.println(message); } } |
三、枚举 + 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // 接口定义规范 public interface Feature { String action(); } // 枚举类 public enum Season implements Feature { SPRING { @Override public String action() { return "春天播种希望" ; } }, SUMMER { @Override public String action() { return "夏天挥洒汗水" ; } }, AUTUMN { @Override public String action() { return "秋天收获喜悦" ; } }, WINTER { @Override public String action() { return "冬天万籁寂静" ; } }; } // 测试类 public class Client { public static void main(String[] args) { String action = Season.SPRING.action(); System.out.println(action); } } |
一、枚举使用规则及注意事项
1、使用 enum 定义的枚举类默认继承 java.lang.Enum 抽象类,由于 java 是单继承的,所以枚举类不能再继承其它类
2、使用 enum 定义的枚举类默认使用 final 进行修饰,final 修饰的类不可以被继承
3、枚举类的构造器只能使用 private 权限修饰符
4、枚举类的所有实例必须在枚举类中显示列出,多个对象之间使用逗号(,)进行分隔,末尾使用分号(;)结尾
5、必须在枚举类的第一行声明枚举类实例
6、如果枚举类只有一个枚举对象,则可以作为一种单例模式的实现方式
二、自定义枚举的方式
1、使用 enum 关键字定义单个枚举类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public enum Novel { // 枚举类的实例必须写在枚举类的最前面 HONG_LOU_MENG( 1 , "红楼梦" ), XI_YOU_JI( 2 , "西游记" ), SHUI_HU_ZHUAN( 3 , "水浒传" ), SAN_GUO_YAN_YI( 4 , "三国演义" ); private Integer type; private String name; // 构造器默认使用 private 修饰,如果要显示的加上权限修饰符,那么只能使用 private 修饰 private Novel(Integer type, String name) { this .type = type; this .name = name; } // 由于枚举类是常量,所以只有 get 方法,不建议定义 set 方法 public Integer getType() { return type; } public String getName() { return name; } } |
2、使用 interface 定义多个枚举类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public interface Books { enum Novel { HONG_LOU_MENG( 1 , "红楼梦" ), XI_YOU_JI( 2 , "西游记" ), SHUI_HU_ZHUAN( 3 , "水浒传" ), SAN_GUO_YAN_YI( 4 , "三国演义" ); private Integer type; private String name; private Novel(Integer type, String name) { this .type = type; this .name = name; } public Integer getType() { return type; } public String getName() { return name; } } enum ProgramLanguage { PHP( 1 , "红楼梦" ), JAVA( 2 , "西游记" ), PYTHON( 3 , "水浒传" ), JAVA_SCRIPT( 4 , "三国演义" ); private Integer category; private String name; ProgramLanguage(Integer category, String name) { this .category = category; this .name = name; } public Integer getCategory() { return category; } public String getName() { return name; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?