与Enum类型混淆 [英] Confusion with Enum type

查看:248
本文介绍了与Enum类型混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我定义一个枚举时,我传递一个字段名称列表,然后不知何故那些字段名称(例如Days.MONDAY)最终引用字段?我可以传递一个字段(例如Days.MONDAY)然后使用开关来获取字段值。更奇怪的是,当我声明枚举字段时,我甚至不必将名字放在引号中,即使它们实际上是值。

Why is it that when I define an enum, I pass it a list of field names, and then somehow those field names (e.g. Days.MONDAY) end up referring to field values? I can pass along a field (e.g. Days.MONDAY) and then use a switch to get the field value. Even more strange, when I declare the enum fields, I don't even have to put the names in quotes, even though they are actually values.

推荐答案

将Java枚举视为定义类的一种很好的语法。以下是可能有用的shell脚本:

Think of the Java enum as a nice syntax for defining a class. Here is a shell transcript that might help:

$ cat > Direction.java
public enum Direction {NORTH, EAST, SOUTH, WEST}
$ javac Direction.java 
$ javap Direction
Compiled from "Direction.java"
public final class Direction extends java.lang.Enum{
    public static final Direction NORTH;
    public static final Direction EAST;
    public static final Direction SOUTH;
    public static final Direction WEST;
    public static Direction[] values();
    public static Direction valueOf(java.lang.String);
    static {};
}

所以是的,你可以说 EAST 是类 Direction 的字段,其值是类 Direction 的实例。我们将此值称为 Direction.EAST ,就像通过任何其他类的静态字段引用值一样。

So yes, you can say EAST is a field of the class Direction and its value is an instance of class Direction. We refer to this value as Direction.EAST the same way you refer to a value through a static field of any other class.

也许这种混淆可能是因为你没有看到类似的声明

Perhaps the confusion is a result of the fact that you don't see a declaration like

public static final Direction.EAST = SOMETHING_OR_OTHER_HERE;

这是可以理解的。枚举用于初始化这些字段值,但您不必自己显式初始化它们。就像你说的那样

This is understandable. Enums are designed to initialize these field values, but you don't have to explicitly initialize them yourself. It is as if you said

public static final Direction.EAST = new Direction();

这实际上是所谓的Typesafe Enum模式的一部分这是在Java获得当前枚举语法之前常用的。

which is, as a matter of fact, a part of what is called the "Typesafe Enum" pattern which was commonly used before Java got the current enum syntax.

关于你对引号的评论,根本没有理由引用任何东西,因为没有涉及字符串。

Regarding your comment about quotes, there is simply no reason to have to quote anything, because there are no strings involved.

关于你对开关的评论,是的,字段名称和字段值的概念如何在这里发挥作用有点有趣。但这仅仅是因为该值看起来像字段。换句话说, Direction 中字段 EAST 的值是, Direction.EAST 。但是,如果你考虑它,它与文字没有太大区别。您可能会问, Float.NaN 的价值是多少?好吧,它是.... Float.NaN 。一种不透明的价值。

Regarding your comment about switches, yes, it is somewhat interesting how the notion of field name and field value play out here. But that is only because the value looks like the field. In other words, the value of field EAST in Direction is, well, Direction.EAST. If you think about it, though, it is not too different from literals. What is the value of Float.NaN you may ask? Well, it is.... Float.NaN. Kind of an opaque value.

这篇关于与Enum类型混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆