java - 在 Java 中实现枚举类的最佳方式

标签 java enums

我正在尝试使用我所有的静态枚举来实现一个文件的最佳方式,而不使用任何 getter 和 setter,仅使用静态信息,我在 PHP 中实现了这一点,如下例所示,你真的需要 getter 和Java 中的 setter ?

final class EnumTrade {
    const buy = 1;
    const sell = 2;
}

final class EnumGender {
    const male = 1;
    const female = 2;
}

final class EnumHttpMethod {
    const get = 1;
    const post = 2;
}

最佳答案

public enum EnumTrade {
  BUY, SELL
}

等等。

编辑:如果数字很重要,请执行:

public enum EnumTrade {
  BUY(1), SELL(2)
}

关于java - 在 Java 中实现枚举类的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18617050/

相关文章:

java - 使用 h2 和 spring boot 进行测试

java - 将路径保存在字符串中

java - 为什么 Oracle Java 提供的答案在寻址枚举集时使用私有(private)静态修饰符?

enums - 匹配包含附件数据的枚举的所有变体

c++ - 类中枚举的前向声明?

ios - swift : Recursively calling a function with an enum type as a parameter

com - "enum"可以用作 COM 中的标志吗?

java - 解密错误填充错误

java - 如何设置 ProGuard 规则使我的核心更难进行逆向工程

java - 如何生成@CreatedDate LocalDateTime 作为时间戳?