1

我需要获取枚举常量的字段值。

例如我有这个枚举:

public enum TestEnum {
  AAA("text 1", 10),
  BBB("text 2", 20);

  private final String text;
  private final int weight;

  TestEnum(String text, int weight) {
    this.text = text;
    this.weight = weight;
  }

  public String getText() {
    return this.text;
  }

  public String getWeight() {
    return this.weight;
  }
}

枚举常量可以通过方法 enumConstants() 访问。

如何获得具体枚举常量的声明值?(我需要枚举常量 AAA 具有字段文本的“文本 1”和字段权重的 10 的信息)。

4

0 回答 0