java创建枚举类

public enum CustomerYesEnum {

    Yes("是", 1),
    No("否", 0),
    ;
    CustomerYesEnum(String code, Integer desc) {
        this.code = code;
        this.desc = desc;
    }

    private String code;
    private Integer desc;


    public String getCode() {
        return code;
    }

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

    public Integer getDesc() {
        return desc;
    }

    public static Integer getDescByCode(String code) {
        for (CustomerYesEnum value : CustomerYesEnum.values()) {
            if (value.code.equals(code)) {
                return value.desc;
            }
        }
        return null;
    }

    public static CustomerYesEnum getStatusByCode(String code) {
        for (CustomerYesEnum value : CustomerYesEnum.values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,可以使用关键字`enum`来创建枚举类。下面是一个简单的示例代码: ```java public enum Color { RED, GREEN, BLUE; } ``` 在上面的示例中,我们创建了一个名为`Color`的枚举类,它包含了三个枚举常量:`RED`、`GREEN`和`BLUE`。你可以根据自己的需求添加更多的枚举常量。 枚举类可以像其他类一样拥有字段、方法和构造函数。你可以根据需要在枚举常量后面添加字段和方法。下面是一个稍复杂的示例: ```java public enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6), JUPITER(1.9e+27, 7.1492e7), SATURN(5.688e+26, 6.0268e7), URANUS(8.686e+25, 2.5559e7), NEPTUNE(1.024e+26, 2.4746e7); private final double mass; // 质量 private final double radius; // 半径 private final double G = 6.67300E-11; // 万有引力常数 Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double getMass() { return mass; } public double getRadius() { return radius; } public double surfaceGravity() { return G * mass / (radius * radius); } public double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); } } ``` 在上面的示例中,我们创建了一个名为`Planet`的枚举类,它包含了一些行星的枚举常量,每个常量都有对应的质量和半径。此外,它还定义了一些方法来计算行星的表面重力和表面重量。 希望这个示例能帮助你理解如何在Java创建枚举类

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

克里斯i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值