简体   繁体   English

Java中的枚举,如C#

[英]Enum in Java like in C#

I would like to have somethimng 我想要一些东西

public enum CategoryTypes 
{
        Unknown = 0,
        One = 1,
        Two = 2,
}

in Java? 在Java中? So it could be possible to cast Int to value and vise versa. 因此,可以将Int转换为价值,反之亦然。 Is it possible? 可能吗?

Enums in Java are actually classes Singleton instances of the class represented by the enum you created. Java中的枚举实际上是您创建的枚举表示的类的单例实例。 For the instances to have values, you need to store it in the class, and create a private constructor to set them. 为了使实例具有值,您需要将其存储在类中,并创建一个私有构造函数来设置它们。 Also a getter method would be nice. 另外,getter方法也很好。 For example: 例如:

  public enum MyEnum {
      INSTANCE(5),
      N2(2);

     private int member;
     private MyEnum(int member) {
       this.member = member;
     }
  }

tl;dr tl; dr

int i = Fruit.GRAPE.ordinal() ;  // Beware: Horrible misnomer; actually a zero-based index rather than an ordinal.

…and… …和…

Fruit f = Fruit.values()[i] ;

But do not do this! 但是不要这样做! Read on for details. 继续阅读以获取详细信息。

Java Enum Java Enum

Java provides a vastly more useful, flexible, and powerful version of enums. Java提供了更为有用,灵活和强大的枚举版本。

See: 看到:

Can be as simple as: 可以很简单:

public enum Fruit {
    APPLE , 
    ORANGE , 
    GRAPE
}

To use it: 要使用它:

Fruit f = Fruit.APPLE ;

Usually we have no need for the underlying integer number representing the order in which the elements were defined for the enum. 通常,我们不需要代表整数的元素的基本整数顺序。 Instead we just use each element of the enum as a normal Java object rather than juggling a mere int number. 相反,我们只是将枚举的每个元素用作普通的Java对象,而不是仅仅处理int数。 But if you insist: 但是,如果您坚持:

  • Call Enum::ordinal to retrieve the a zero-based index number (this method is a terrible misnomer as it is an index rather than a one-based ordinal) 调用Enum::ordinal以检索从零开始的索引号(此方法是一个糟糕的误称,因为它是一个索引而不是基于一的序数)
    int i = Fruit.GRAPE.ordinal() ;
  • Call Enum::values to generate an array which you can access by zero-based index 调用Enum::values生成一个数组,您可以通过从零开始的索引进行访问
    Fruit f = Fruit.values()[i] ;

In doing so, almost certainly you would be failing to take advantage of the full power of the Java enum facility. 这样做几乎可以肯定, 您将无法利用Java枚举功能的全部功能。 Using the actual enum objects rather than a mere int gives you type-safety, ensures valid values, and makes your code more self-documenting. 使用实际的枚举对象而不是单纯的int为您提供类型安全性,确保有效值并使您的代码更具自记录性。

If your intent is to assign a meaningful code number to each enum instance, do so explicitly by defining a member variable populated by constructor taking that code number, as discussed here and here . 如果您打算为每个枚举实例分配有意义的代码号,请通过定义由构造函数使用该代码号填充的成员变量来明确地进行分配,如此此处所述

You can add methods and even add constructors taking arguments. 您可以添加方法,甚至可以添加带有参数的构造函数。 Even more to it: EnumSet and EnumMap are specialized collections optimized for very little memory and very fast execution. 更重要的是: EnumSetEnumMap是经过优化的专用集合,其内存极少且执行速度非常快。

Set< Fruit > favoriteFruits = EnumSet.of( Fruit.APPLE , Fruit.ORANGE ) ;

Oddly, the EnumSet does not implement SortedSet , yet it is documented as returning instances in the order of their enum definition. 奇怪的是, EnumSet没有实现SortedSet ,但是按照其枚举定义的顺序将其记录为返回实例。

Search Stack Overflow for many more discussions and examples. 搜索堆栈溢出以获取更多讨论和示例。

For a prime example of Enum in action, see: 有关运行中Enum的主要示例,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM