简体   繁体   English

是Java中的枚举类型

[英]Is enum a type in Java

I finally have a reason to implement an enum in Java and and find it to be an invalid type in 1.6. 我终于有理由在Java中实现一个enum ,并在1.6中发现它是无效的类型。 So I declare the enum as 所以我宣布枚举为

public enum MyEnum = {A=0, B=1, C=3}; 

and get an error invalid type in Eclipse. 并在Eclipse中获取错误无效类型。

That's not how they are used. 那不是他们的用法。 See examples at http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html , such as 请参见http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html上的示例,例如

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

Java enums are much more powerful than in, say, C#. Java枚举比C#强大得多。 They're (almost) full-blown objects, and thus cannot be represented with just a number. 它们是(几乎)成熟的对象,因此不能仅用数字来表示。 They can however contain a number, and they do have an ordinal number value . 但是,它们可以包含数字,并且确实具有序数值

You could have the number value with 你可以用

public enum MyEnum {
    A(0), B(1), C(3);

    private final int number;
    MyEnum(int number) {
      this.number = number;
    }
}

Java enums are quite different to C and C++ enums . Java enums与C和C ++ enums完全不同。 The biggest difference is that the enum constants are full-blown objects rather than compile-time integer constants. 最大的区别是enum常量是成熟的对象,而不是编译时整数常量。

In your example, A , B and C are objects. 在您的示例中, ABC是对象。 Therefore constructs like A=0, B=1, C=3 are not allowed. 因此A=0, B=1, C=3不允许使用A=0, B=1, C=3结构。

I recommend taking a look at the tutorial . 我建议看一下该教程

If you have to associate numeric values with enum constants, you can achieve a similar effect like so: 如果必须将数值与enum常量关联,则可以实现类似的效果:

public enum MyEnum {
    A(0), B(1), C(3);
    public final int val;
    private MyEnum(int val) {
        this.val = val;
    }
}

Having done this, you can access the values using MyEnum.A.val etc. 完成此操作后,您可以使用MyEnum.A.val等访问值。

In Java, enum is a type, but it is incompatible with integers. 在Java中, enum是一种类型,但与整数不兼容。 Enum objects are closer to classes than to primitives, in that they can have methods, member variables, and so on. 枚举对象比基本类型更接近类,因为它们可以具有方法,成员变量等。

Java lets you get the effect that you wanted by adding an int member to your enumeration, and initializing it differently for different enumeration members, like this: Java通过向枚举添加一个int成员,并针对不同的枚举成员以不同的方式对其进行初始化,从而获得所需的效果,如下所示:

public enum MyEnum {
    A(1), B(2), C(3);
    int val;
    private MyEnum (int v) {
        val = v;
    }
    public int value() {
        return val;
    }
};

Now each member of your enum has a public method value() that returns the integer value associated with the corresponding element of the enumeration. 现在, enum每个成员都有一个公共方法value() ,该方法返回与枚举的相应元素关联的整数值。

you can't do this as you do. 你不能像你那样做。 enum define in java in the following way enum在Java中的定义方式如下

enum myenum{a,b,c,d}
public enum ModuleType {A,B,C}

To answer your question: enum is technically not a type. 回答您的问题:从技术上来说, enum不是一种类型。 Therefore you cannot define a variable of type enum as you have tried. 因此,您无法像尝试那样定义类型为enum的变量。

enum is a keyword to define an enumerated (abbreviated to enum) type which is a special data type. enum是用于定义枚举(缩写为enum)类型的关键字,它是一种特殊的数据类型。 Once an enum type is defined (very much like how you would define a class), you refer to it using the name of the enum type. 定义枚举类型后(非常类似于您定义类的方式),您可以使用枚举类型的名称来引用它。 Please check the enum types documentation . 请检查枚举类型文档

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

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