枚举类型较传统定义常量的方式,除了具有参数类型检测的优势之外,还具有其他方面的优势。

用户可以将一个枚举类型看作是一个类,它继承于 java.lang.Enum 类,当定义一个枚举类型时,每一个枚举类型成员都可以看作是枚举类型的一个实例,这些枚举类型成员都默认被final、 public、static修饰,所以当使用枚举类型成员时直接使用枚举类型名称调用枚举类型成员即可。
| package com.example.enumerate; |
| |
| |
| import javax.swing.*; |
| |
| |
| interface Constants{ |
| public static final int Constatnts_A=1; |
| public static final int Constatnts_B=12; |
| } |
| |
| |
| public class ConstantsTest{ |
| enum Constants2{ |
| Constants_A, Constants_B |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static void doit2(Constants2 c){ |
| switch (c){ |
| case Constants_A: |
| System.out.println("doit()1 Constants_A"); |
| break; |
| case Constants_B: |
| System.out.println("doit2()1 Constants_B"); |
| break; |
| } |
| } |
| |
| public static void main(String[] args) { |
| |
| |
| |
| ConstantsTest.doit2(Constants2.Constants_A); |
| |
| ConstantsTest.doit2(Constants2.Constants_B); |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |
| public class EnumIndexTest { |
| enum Constants2{ |
| |
| Constants_A("我是枚举成员A"), |
| Constants_B("我是枚举成员B"), |
| Constants_C("我是枚举成员C"), |
| Constants_D(3); |
| |
| private String description; |
| private int i = 4; |
| private Constants2(String description){ |
| |
| this.description = description; |
| } |
| |
| private Constants2(int i){ |
| this.i=this.i+i; |
| } |
| public String getDescription(){ |
| return description; |
| } |
| public int getI(){ |
| return i; |
| } |
| } |
| |
| public static void main(String[] args) { |
| for (int i = 0; i < Constants2.values().length; i++) { |
| System.out.println(Constants2.values()[i]+"调用getDescription()方法为: " |
| +Constants2.values()[i].getDescription()); |
| |
| } |
| System.out.println(Constants2.valueOf("Constants_D")+"调用getI()方法为: " |
| +Constants2.valueOf("Constants_D").getI()); |
| } |
| } |
- 还可以将这个getDescription()方法放置在接口中,使枚举类型实现该接口,然后使每个枚举类型实现接口中的方法。代码如下:
| interface d{ |
| public String getDescription(); |
| public int getI(); |
| } |
| |
| public enum AnyEnum implements d{ |
| Constants_A{ |
| |
| public String getDescription(){ |
| return ("枚举成员A"); |
| } |
| public int getI(){ |
| return i; |
| } |
| }, |
| |
| Constants_B{ |
| |
| public String getDescription(){ |
| return ("枚举成员B"); |
| } |
| public int getI(){ |
| return i; |
| } |
| }, |
| |
| Constants_C{ |
| |
| public String getDescription(){ |
| return ("枚举成员C"); |
| } |
| public int getI(){ |
| return i; |
| } |
| }, |
| |
| Constants_D{ |
| |
| public String getDescription(){ |
| return ("枚举成员D"); |
| } |
| public int getI(){ |
| return i; |
| } |
| }, |
| Constants_E{ |
| @Override |
| public String getDescription() { |
| return null; |
| } |
| |
| @Override |
| public int getI() { |
| return 0; |
| } |
| }; |
| private static int i=5; |
| |
| public static void main(String[] args) { |
| for (int i = 0; i < AnyEnum.values().length; i++) { |
| System.out.println(AnyEnum.values()[i]+"调用getDescription()方法: " |
| +AnyEnum.values()[i].getDescription()); |
| System.out.println(AnyEnum.values()[i]+"调用Get()方法为: " |
| +AnyEnum.values()[i].getI()); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了