Java:如何在其类型定义中修改当前枚举的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3335398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:23:05  来源:igfitidea点击:

Java: How to modify value of current enum inside of its type definition?

java

提问by zer0stimulus

I defined an enum type that implements an interface as follows:

我定义了一个实现接口的枚举类型,如下所示:

public enum MyEnum implements MyInterface
{
    val1, val2, val3;
    private MyEnum() {}
    private MyEnum(Parcel in)
    {
        readFromParcel(in);
    }

    public void readFromParcel(Parcel in)
    {
        MyEnum val = MyEnum.values()[in.readInt()];
        // ??? How to I assign val to my current enum?
    }

}

How do I access the value of the current enum object so I can make the assignment inside of readFromParcel()? (Please see comment in code)

如何访问当前枚举对象的值,以便我可以在其中进行赋值readFromParcel()?(请参阅代码中的注释)

采纳答案by polygenelubricants

Inside an instance method, you can refer to the "current enumobject" as simply this. It works this way because enumconstants are actual objects, i.e. instances of a class-- a very special type of class, but a classnonetheless. (Note that I mentioned that these are enumconstants: it is the convention in Java to use all uppercase letters when naming constants.)

在实例方法中,您可以enum简单地将“当前对象”称为this. 它以这种方式工作,因为enum常量是实际对象,即 a 的实例class—— 一种非常特殊的 类型class,但class仍然是 a 。(请注意,我提到这些是enum常量:Java 中的约定是在命名常量时使用所有大写字母。)

Your usage of values()is also very peculiar (not to mention that it'll perform horribly since a new array must be constructed at each call). Perhaps you'd want to take a look at EnumMap, which is a special kind of Mapoptimized for enumconstants as keys.

您的使用values()也非常奇特(更不用说它的性能会很糟糕,因为每次调用都必须构造一个新数组)。也许您想看看EnumMap,这是一种Map针对enum常量作为键进行了优化的特殊类型。

If you're trying to mutate fields contained in these enumconstants, then you should seriously consider a redesign. You should generally minimize mutability anyway, but having these staticsingletons be mutable does not sound like a good design. Instead of having these mutable fields intrinsic within the enumconstants themselves, a Mapfrom the constants to these mutable values would be a much better design.

如果您试图改变包含在这些enum常量中的字段,那么您应该认真考虑重新设计。无论如何,您通常应该尽量减少可变性,但是让这些static单例可变听起来并不是一个好的设计。与其将这些可变字段固有地包含在enum常量本身中,Map不如从常量到这些可变值是一个更好的设计。

See also

也可以看看

  • Java Tutorials/enum
  • Effective Java 2nd Edition
    • Item 15: Minimize mutability
    • Item 31: Use instance fields instead of ordinals
    • Item 32: Use EnumSetinstead of bit fields
    • Item 33: Use EnumMapinstead of ordinal indexing
  • Java教程/enum
  • 有效的 Java 第二版
    • 第 15 条:最小化可变性
    • 第 31 条:使用实例字段而不是序数
    • 第 32 条:使用EnumSet代替位域
    • 第 33 条:使用EnumMap而不是顺序索引

Various questions on Java enum

关于Java的各种问题 enum

回答by Tom Hawtin - tackline

It's really not a good idea to make enums mutable. However, the thisfor an enumis just this.

使枚举可变真的不是一个好主意。但是,thisfor anenum只是this

MyEnum val = this;

The actual fields val1, val2and val3(should be VAL1, VAL2and VAL3) are implicitly public static final.

实际的字段val1val2以及val3(应该是VAL1VAL2VAL3)是含蓄public static final

回答by Devon_C_Miller

Enums are immutable, so you cannot. Your best be is to make readFromParcelstatic and have it return an enum. So, something like this:

枚举是不可变的,所以你不能。你最好的办法是制作readFromParcel静态并让它返回一个枚举。所以,像这样:

    public static MyEnum readFromParcel(Parcel in)
    {
        MyEnum val = MyEnum.values()[in.readInt()];
        return val;
    }

回答by Adrian Regan

Use this.

使用这个

if this.equals(val) {
     ...
}