枚举的表示

发布于 2024-08-14 00:39:00 字数 378 浏览 2 评论 0原文

枚举在编程语言中如何“在幕后”工作?我猜测每种语言都有不同的方式来表示这些数据类型。

在java中,您可以使用==运算符,例如:

public class TestEnum {
   private enum Test {
      foo, bar
   }

   public static void main(String[] args) {
      System.out.println(Test.foo == Test.foo); // returns true
   }
}

在==期间,枚举类型是否转换为原始类型?或者枚举值是单例吗? C# 是否以与 Java 相同的方式利用枚举?与编程语言相比,数据库枚举类型的处理方式是否有所不同?

How do enums work 'behind the scenes' in programming languages? I am guessing that each language has a different way of representing these datatypes.

In java you can use the == operator, for example:

public class TestEnum {
   private enum Test {
      foo, bar
   }

   public static void main(String[] args) {
      System.out.println(Test.foo == Test.foo); // returns true
   }
}

Is an enum type converted to a primitive during the ==? Or is the enum value a Singleton? Does C# leverage enums in the same manner as java? Are database enum types treated differently compared to programming languages?

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

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

发布评论

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

评论(5

少女情怀诗 2024-08-21 00:39:00

Java enum 使用了很多的技巧,使其仍然是对象,但可以与 == 一起使用。原始的类型安全枚举模式(另请参阅有效Java)可以提供一些见解,但是Enum.java source 将向您展示他们现在是如何做到的。

Java enums make use of a lot of tricks to still be objects but work with ==. The original typesafe enum pattern (see also Effective Java) can provide some insight, but the Enum.java source will show you exactly how they do it now.

自找没趣 2024-08-21 00:39:00

Java 中的枚举类型实际上是一个特殊的编译器生成的类,而不是算术类型:枚举值表现为全局预生成实例,以便比较引用来代替 equals。

您可以反汇编 .class 文件来验证它:

  Code:
    0:   getstatic       #2; //Field YourEnum.BAR:LYourEnum;
    3:   getstatic       #3; //Field YourEnum.FOO:LYourEnum;
    6:   if_acmpne       17    

它应该大致相当于以下 Java 代码:

enum YourEnum {
    FOO,
    BAR
}

// ...
if (YourEnum.BAR == YourEnum.FOO)

为了简单起见,您可以将其视为字符串驻留的特殊情况。

An enum type in Java is actually a special compiler-generated class rather than an arithmetic type: enum values behave as global pre-generated instances in order to compare references in place of equals.

You can verify it disassembling a .class file:

  Code:
    0:   getstatic       #2; //Field YourEnum.BAR:LYourEnum;
    3:   getstatic       #3; //Field YourEnum.FOO:LYourEnum;
    6:   if_acmpne       17    

it should roughly equivalent to the following Java code:

enum YourEnum {
    FOO,
    BAR
}

// ...
if (YourEnum.BAR == YourEnum.FOO)

For the sake of simplicity you can think it as a special case of string interning.

高冷爸爸 2024-08-21 00:39:00

我认为大多数语言都会在幕后将枚举转换为 int - 尽管这当然不是必需的。

例如 - 在上面的示例中,编译器当然有可能意识到这两个值相等,而无需将它们转换为某种中间表示形式,而只是发出一个 true 值。

I think that most languages convert enums into int behind the scenes - although that certainly isn't requirement.

For example - in your example above it is certainly possible that the compiler realizes that the two values are equal without ever converting them to some intermediate representation and just emits a true value.

空城旧梦 2024-08-21 00:39:00

我认为枚举只是常量整数。

所以你可以

const int foo = 0;
const int bar = 1;

等编译器向它们添加命名空间“Test”

I would think enums are simply const integers.

so you have

const int foo = 0;
const int bar = 1;

and so on with the compiler adding the namespace 'Test' to them

厌味 2024-08-21 00:39:00

.Net语言以整数的形式表示它们。
如果你这样做

If 1 == foo ,这应该返回 true

我通常更容易理解的是:

public enum Test
    foo = 1
    bar = 2
end enum

尝试一下,用字符串更改 1 和 2 。这应该会引发编译器错误。

.Net language represent them in form of integers.
If you do

If 1 == foo , this should return true

What I would usually to be easier to understand is this :

public enum Test
    foo = 1
    bar = 2
end enum

Give it a try, change the 1 and 2 with strings. This should throw you a compiler error.

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