Java — 枚举

一、枚举简介

枚举是 JDK5 中引入的特性,由 enum 关键字来定义一个枚举类。

格式:

enum 枚举类名 {
    枚举项1, 
    枚举项2, 
    ...;
    
    成员变量;
    构造方法
	成员方法
}

说明:

  1. 每个枚举类型隐式继承 java.lang.Enum ,所以枚举类不能再继承其它类。
  2. 枚举项就是枚举类型的静态常量对象(实例),通过 枚举类型.枚举项名称 访问指定的枚举项。
  3. 枚举项命名一般使用大写字母,多个枚举项用英文逗号隔开,以英文分号结束。
  4. 枚举类型的构造方法需要私有化。

二、枚举作用

一些程序在运行时需要的数据不能是随意的,必须是一定范围内的值。也就是统一管理一些常量。
JDK5 之前采用自定义类来解决,JDK5 之后可以直接采用枚举解决。

示例:

// JDK5之前自定义性别枚举
public class CustomSexEnum {

    public static final CustomSexEnum man = new CustomSexEnum(1, "男");
    public static final CustomSexEnum woman = new CustomSexEnum(2, "女");

    private Integer code;
    private String desc;

    private CustomSexEnum() {
    }

    private CustomSexEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
// JDK5之后定义性别枚举
public enum SexEnum {

    man(1, "男"),
    woman(2, "女");

    private Integer code;
    private String desc;

    private SexEnum() {
    }

    private SexEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

测试:

public class Test_01 {
    public static void main(String[] args) {
        // 获取男性对应代码及描述
        Integer manCode = CustomSexEnum.man.getCode();
        String manDesc = CustomSexEnum.man.getDesc();
        System.out.println("manCode = " + manCode);
        System.out.println("manDesc = " + manDesc);

        // 获取女性对应的代码及描述
        Integer womanCode = SexEnum.woman.getCode();
        String womanDesc = SexEnum.woman.getDesc();
        System.out.println("womanCode = " + womanCode);
        System.out.println("womanDesc = " + womanDesc);
        
        // 枚举就是一个对象
        SexEnum man = SexEnum.man;
        System.out.println(man == SexEnum.man);
    }
}

运行:

manCode = 1
manDesc = 男
womanCode = 2
womanDesc = 女
true

三、枚举实现接口

示例:

// 系统代码接口
public interface SystemCode {
    // 获取代码
    Integer getCode();

    // 获取代码描述
    String getDesc();
}
// 系统代码接口实现类
public enum SystemCodeEnum implements SystemCode {

    ERROR(-1, "系统异常"),
    FAIL(0, "操作失败"),
    SUCCESS(1, "操作成功");

    private Integer code;
    private String desc;

    SystemCodeEnum() {
    }

    SystemCodeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public Integer getCode() {
        return this.code;
    }

    @Override
    public String getDesc() {
        return this.desc;
    }
}

测试:

public class Test_02 {
    public static void main(String[] args) {
        SystemCode systemCode = SystemCodeEnum.SUCCESS;
        System.out.println("code = " + systemCode.getCode());
        System.out.println("desc = " + systemCode.getDesc());
    }
}

运行:

code = 1
desc = 操作成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值