Java:如何在枚举中使用构造函数重载?

发布于 2024-11-27 02:06:41 字数 158 浏览 0 评论 0原文

我正在使用 Java 中的枚举。正如我所看到的,可以重载枚举构造函数。我的问题是,在这种情况下,是否有可能从构造函数重载中受益,因为据我所知,既不可能自己调用​​它,也不可能强制编译器调用您想要调用的特定构造函数?

感谢您花时间向我澄清这些内容,并希望这对可能有相同问题的其他人也有用。

I am working with enumerations in Java. As I can see, it is possible to overload an enumeration constructor. My question is it possible at all to benefit from constructor overloading in this context given that as far as I understand it is possible neither to call it by yourself no to force the compiler to call a particular one that you would like to call?

Appreciate the time your take to clarify that stuff to me and hope it would also be useful for others who might have the same question in mind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嘿嘿嘿 2024-12-04 02:06:41

确实会在设置枚举值时调用它。例如:

public enum Person
{
    FRED("Frederick", "Fred"),
    PETE("Peter", "Pete"),
    MALCOLM("Malcolm"); // No nickname

    private final String nickname;
    private final String name;

    private Person(String name, String nickname)
    {
        this.name = name;
        this.nickname = nickname;
    }

    private Person(String name)
    {
        this(name, name); // Just use the name as the nickname too
    }

    // getNickname and getName
}

我过去曾在各种项目中使用过这种能力。枚举是 Java 最好的功能之一 - 当然,这也是我在 C# 中工作时从 Java 中怀念的为数不多的功能之一。 (我确信它们可以更巧妙地实现,但是......)

You do call it - when setting the enum values. For example:

public enum Person
{
    FRED("Frederick", "Fred"),
    PETE("Peter", "Pete"),
    MALCOLM("Malcolm"); // No nickname

    private final String nickname;
    private final String name;

    private Person(String name, String nickname)
    {
        this.name = name;
        this.nickname = nickname;
    }

    private Person(String name)
    {
        this(name, name); // Just use the name as the nickname too
    }

    // getNickname and getName
}

I've used this ability for various projects in the past. Enums are one of Java's nicest features - certainly one of the few things I miss from Java when working in C#. (They could be implemented even more neatly, I'm sure, but...)

梦里泪两行 2024-12-04 02:06:41

更完整的例子:

public enum EnumTest {
  NO_ARGS, 
  FIRST_ARG("initialized string"), 
  SECOND_ARG(5), 
  BOTH_ARGS("first", Integer.MAX_VALUE);

  private final String one;
  private final int two;

  private EnumTest()
  {
    this.one = "";
    this.two = 2;
  }

  private EnumTest(String one)
  {
    this.one = one;
    this.two = 0;
  }

  private EnumTest(int two)
  {
    this.one = "";
    this.two = two;
  }

  private EnumTest(String one, int two)
  {
    this.one = one;
    this.two = two;
  }
}

Even more complete example:

public enum EnumTest {
  NO_ARGS, 
  FIRST_ARG("initialized string"), 
  SECOND_ARG(5), 
  BOTH_ARGS("first", Integer.MAX_VALUE);

  private final String one;
  private final int two;

  private EnumTest()
  {
    this.one = "";
    this.two = 2;
  }

  private EnumTest(String one)
  {
    this.one = one;
    this.two = 0;
  }

  private EnumTest(int two)
  {
    this.one = "";
    this.two = two;
  }

  private EnumTest(String one, int two)
  {
    this.one = one;
    this.two = two;
  }
}
月下伊人醉 2024-12-04 02:06:41

是的,您可以使用构造函数重载:

public enum SomeEnum {
  VALUE1("foo"),
  VALUE2("bar", "baz");

  public final String v1;
  public final String v2;

  SomeEnum(final String v) {
    this(v, v);
  }

  SomeEnum(final String v1, final String v2) {
    this.v1 = v1;
    this.v2 = v2;
  }
}

Yes, you can use constructor overloading:

public enum SomeEnum {
  VALUE1("foo"),
  VALUE2("bar", "baz");

  public final String v1;
  public final String v2;

  SomeEnum(final String v) {
    this(v, v);
  }

  SomeEnum(final String v1, final String v2) {
    this.v1 = v1;
    this.v2 = v2;
  }
}
失去的东西太少 2024-12-04 02:06:41

当您定义枚举“常量”时,您可以使用构造函数

enum Eg {
   ZERO, ONE(1), TWO(2,2);

   Eg() { this(0); }
   Eg(int i) { this(i, 0); }
   Eg(int i, int j) { }
}

You use the constructor when you define your enum "constants"

enum Eg {
   ZERO, ONE(1), TWO(2,2);

   Eg() { this(0); }
   Eg(int i) { this(i, 0); }
   Eg(int i, int j) { }
}
三五鸿雁 2024-12-04 02:06:41

有没有办法使用构造函数来从其等效的整数值中获取枚举?目前我有这样的代码,但是这个长的 switch 语句不是很简洁。

ZoneGroup zoneGroup = ZoneGroup.get(loader.getId());

public static enum ZoneGroup {
    ANDROID_NOTABLE(0), ANDROID_TOP(1), ANDROID_ALL(2), ANDROID_NEAR(3), ANDROID_FAV(4), UNDEFINED(5);

    private final int value;

    ZoneGroup(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static ZoneGroup get(int value){
        switch (value) {
        case 0:
            return ANDROID_NOTABLE;
        case 1:
            return ANDROID_TOP;
        case 2:
            return ANDROID_ALL;
        case 3:
            return ANDROID_NEAR;
        case 4:
            return ANDROID_FAV;
        default:
            return UNDEFINED;
        }
    }
}

Is there a way to use constructor in order to get enum from it's equivalent integer value? Currently I have such code, but this long switch statement is not very concise.

ZoneGroup zoneGroup = ZoneGroup.get(loader.getId());

public static enum ZoneGroup {
    ANDROID_NOTABLE(0), ANDROID_TOP(1), ANDROID_ALL(2), ANDROID_NEAR(3), ANDROID_FAV(4), UNDEFINED(5);

    private final int value;

    ZoneGroup(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static ZoneGroup get(int value){
        switch (value) {
        case 0:
            return ANDROID_NOTABLE;
        case 1:
            return ANDROID_TOP;
        case 2:
            return ANDROID_ALL;
        case 3:
            return ANDROID_NEAR;
        case 4:
            return ANDROID_FAV;
        default:
            return UNDEFINED;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文