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
Java: How to modify value of current enum inside of its type definition?
提问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 enum
object" as simply this
. It works this way because enum
constants are actual objects, i.e. instances of a class
-- a very special type of class
, but a class
nonetheless. (Note that I mentioned that these are enum
constants: 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 Map
optimized for enum
constants as keys.
您的使用values()
也非常奇特(更不用说它的性能会很糟糕,因为每次调用都必须构造一个新数组)。也许您想看看EnumMap
,这是一种Map
针对enum
常量作为键进行了优化的特殊类型。
If you're trying to mutate fields contained in these enum
constants, then you should seriously consider a redesign. You should generally minimize mutability anyway, but having these static
singletons be mutable does not sound like a good design. Instead of having these mutable fields intrinsic within the enum
constants themselves, a Map
from 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
- Java教程/
enum
- 有效的 Java 第二版
Various questions on Java enum
关于Java的各种问题 enum
回答by Tom Hawtin - tackline
It's really not a good idea to make enums mutable. However, the this
for an enum
is just this
.
使枚举可变真的不是一个好主意。但是,this
for anenum
只是this
。
MyEnum val = this;
The actual fields val1
, val2
and val3
(should be VAL1
, VAL2
and VAL3
) are implicitly public static final
.
实际的字段val1
,val2
以及val3
(应该是VAL1
,VAL2
和VAL3
)是含蓄public static final
。
回答by Devon_C_Miller
Enums are immutable, so you cannot. Your best be is to make readFromParcel
static 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) {
...
}