Java 枚举值

发布于 2024-12-14 08:39:19 字数 244 浏览 1 评论 0原文

枚举的值是如何计算的?编译器是否给它一个静态值?它是什么样的价值?

我问的原因是因为我想知道通过 tcp(或任何进程间)连接使用枚举值会产生什么后果。 (我所说的枚举值是指Enum.VALUE)显然,除非您知道每个枚举的预先确定的值,否则您将无法从其他语言构造与您的程序兼容的数据包。那么有可能找出每个的“真实”值吗?两个使用相同枚举“类”文件的 Java 程序会互相理解吗?

这纯属理论,请勿骂。不过,指针是好的。

How is the value of an enum calculated? Does the compiler give it a static value? What kind of value is it?

The reason I ask is because I was wondering what the consequences of using enum values over a tcp (or any inter-process) connection would be. (By enum value I mean Enum.VALUE) Obviously you wouldn't be able to construct packets compatible with your program from another language unless you knew the pre-determined values of each enum. So is it possible to figure out the "real" values of each? And will two Java programs using the same enum "class" file understand each other?

This is purely theoretical, please no scoldings. Pointers are good, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窗影残 2024-12-21 08:39:19

正如已经解释的,不要依赖底层实现。相反,设计您的枚举以将此类信息存储在您的控制之下。想象一下,您有三个命令要发送到某个嵌入式控制器,通过将位模式写入寄存器来完成。您希望使用枚举来提供命令的高级表示,但在某些时候您需要那些讨厌的位模式:

public enum EmbeddedControllerCommand {

    FOO(0x001), BAR(0x010), BAZ(0x100);

    private int bitPattern = 0;

    EmbeddedControllerCommand(int bitPattern) {
        this.bitPattern = bitPattern;
    }

    public int getBitPattern() {
        return this.bitPattern;
    }

}

只需调用 EmbeddedControllerCommand.FOO.getBitPattern() 即可获取命令模式FOO 命令。

As has been explained, don't rely on underlying implementations. Instead, design your enum to store this sort of information under your control. Imagine you have three commands to send to some embedded controller, done by writing a bit pattern to a register. You want to use enums to give you a high-level representation of the commands, but at some point you need those pesky bit patterns:

public enum EmbeddedControllerCommand {

    FOO(0x001), BAR(0x010), BAZ(0x100);

    private int bitPattern = 0;

    EmbeddedControllerCommand(int bitPattern) {
        this.bitPattern = bitPattern;
    }

    public int getBitPattern() {
        return this.bitPattern;
    }

}

Just call EmbeddedControllerCommand.FOO.getBitPattern() to get the command pattern for the FOO command.

失去的东西太少 2024-12-21 08:39:19

Java 枚举中确实有 Java 称为序数的整数引用。例如,您可以执行 yourEnumVariable.ordinal() 来获取枚举变量的 int 编号。编号的分配方式是列表中第一个编号为 0,第二个编号为 1,依此类推。

然而,就您而言,听起来您不应该关心枚举的内部表示是什么。您应该决定使用哪种协议来传输信息。之后你就可以考虑如何传递枚举值了。无论如何,您可能必须为协议构建一些映射类,以从 Java 映射到协议。

in Java Enums do have integer reference that Java calls ordinal. For example you can do yourEnumVariable.ordinal() to get the int number of the enum variable. The numbers are assigned in a way that first in the list gets number 0, second number 1 and so on.

However in your case it sounds like you should not be concerned about what is the internal representation of enums. You should decide on what kind of protocol you will use to transfer information. After that you can think of how to transfer enum values. You'll probably have to anyway build some mapping classes for the protocol to map from Java to the protocol.

童话 2024-12-21 08:39:19

如果您参考 Enum JavaDoc 您可以阅读 Enum.ordinal( ) 方法返回一个数字常量值,由类文件中 Enum 声明的顺序确定。

例如,按字母顺序考虑这些枚举常量:

public enum Fruit{
    APPLE, // APPLE.ordinal()  == 0
    ORANGE;// ORANGE.ordinal() == 1
    PEAR,  // PEAR.ordinal()   == 2
}

但是,如果我们在其字母顺序位置添加 BANANA,请查看这些值将如何变化,从而破坏依赖它们的任何代码:

public enum Fruit{
    APPLE, // APPLE.ordinal()  == 0
    BANANA,// BANANA.ordinal() == 1
    ORANGE;// ORANGE.ordinal() == 2
    PEAR,  // PEAR.ordinal()   == 3
}

这意味着,只要您使用相同的版本对于源代码,您可以依赖相同的基础值,并且可以在没有其他选项的情况下使用它。我之所以说没有其他选择,是因为通过使代码依赖于枚举的顺序,它会使代码更加脆弱,而如果您不依赖序数值,则可以随意重新排序现有的或插入新的枚举常量。

现在,您会接受因在发帖前没有彻底阅读 JavaDocs 而受到的温和责骂吗? :)

If you refer to the Enum JavaDoc you can read that the Enum.ordinal() method returns a numeric constant value which is determined by the order of the Enum declarations in the class file.

For example, consider these enum constants in alphabetical order:

public enum Fruit{
    APPLE, // APPLE.ordinal()  == 0
    ORANGE;// ORANGE.ordinal() == 1
    PEAR,  // PEAR.ordinal()   == 2
}

But if we add BANANA in its alphabetical position, see how the values would change, breaking any code that relies on them:

public enum Fruit{
    APPLE, // APPLE.ordinal()  == 0
    BANANA,// BANANA.ordinal() == 1
    ORANGE;// ORANGE.ordinal() == 2
    PEAR,  // PEAR.ordinal()   == 3
}

This means that, so long as you're using the same version of the source code, you can rely on the underlying value being the same, and can use it for situations where there are no other options. I say no other options because by making your code dependent on the order of the enums, it makes the code more fragile, whereas if you aren't relying on the ordinal value you can reorder existing or insert new enum constants at will.

Now, would you accept a mild scolding for not reading the JavaDocs thoroughly before post? :)

暗藏城府 2024-12-21 08:39:19

你不应该依赖 Java 编译器给出的 enum 的预分配值。不同的语言有不同的处理枚举的方案。在 Java 中,枚举可以被视为类和对象(不完全相同的目的),因此您可以在枚举中创建构造函数并将枚举初始化为您想要的值。这是一个相当清晰的示例:

http://download.oracle.com/ javase/tutorial/java/javaOO/enum.html

You should not rely the pre-assinged value of enum given by Java compiler. Different language has different scheme to deal with enum. In Java, enum can be treated like a class and objects (not exactly for the same purpose) so you can create a constructor in your enum and initialize the enum to the value you want. Here is a fairly clear examples:

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文