除了枚举声明之外,拥有更多枚举类型实例有什么缺点?

发布于 2024-10-14 10:25:42 字数 187 浏览 10 评论 0原文

尝试显式实例化枚举类型是一个编译时错误

(第 15.9.1 节)。 Enum 中的最终克隆方法确保枚举常量永远不会被克隆,并且序列化机制的特殊处理确保永远不会因反序列化而创建重复实例。禁止枚举类型的反射实例化。这四件事共同确保枚举类型的实例不存在于枚举常量定义的实例之外。

除了枚举之外,拥有更多枚举类型实例的缺点是什么 宣言?

It is a compile-time error to attempt to explicitly instantiate an enum type

(§15.9.1). The final clone method in Enum ensures that enum constants can never be cloned, and the special treatment by the serialization mechanism ensures that duplicate instances are never created as a result of deserialization. Reflective instantiation of enum types is prohibited. Together, these four things ensure that no instances of an enum type exist beyond those defined by the enum constants.

What is the drawback of having more instances of enum type other than in the enum
declaration?

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

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

发布评论

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

评论(5

总以为 2024-10-21 10:25:42

缺点是你放弃了枚举的静态可检查性。

您可以静态地确保枚举值上的switch确实可以处理所有情况。您可以静态地确保使用枚举调用方法是正常的。您可以静态地确保资源包中的每个枚举值都有翻译。还有更多的事情。

您可以确保 equals() 与带有枚举的 == 等效。

所有这些都定义一个枚举

如果您想要与枚举“类似”的东西,那么就写它:它并不太难。

The drawback is that you're throwing away the statically checkability of enums.

You can statically ensure that a switch on an enum value does indeed handle all cases. You can statically ensure that calling a method with an enum does a sane thing. You can statically ensure that there's a translation of every enum value in your resource bundles. And many more things.

You can make sure that equals() is equivalent to == with enums.

All of this defines an enum.

If you want something "similar" to an enum, then write it: it's not too hard.

孤檠 2024-10-21 10:25:42

这会打破比较。您想要

if (myEnumValue == yourEnumValue)
{
}

工作,如果可能存在相同枚举值的多个实例,则不能保证像这样的基于引用的测试能够工作。与字符串进行比较。

It would break comparisons. You want

if (myEnumValue == yourEnumValue)
{
}

to work, if there could be multiple instances of the same enum value, there's no guarantee that reference-based testing like this would work. Compare with strings.

山田美奈子 2024-10-21 10:25:42

好吧,我想这会引起混乱,因为人们希望能够使用 == 与枚举进行比较。能够克隆会破坏语义。

Well, I guess it would cause confusion, because people expect to be able to compare against enums using ==. Being able to clone would break the semantics.

黑色毁心梦 2024-10-21 10:25:42

如果您有两个枚举,那么以下指令可能是 true 或 false...

public boolean isEnumBlack(EnumType enum) {
    if (enum == EnumType.BLACK) {
        return true;
    } else {
        return false;
    }
}

然后,如果您使用 EnumType 的“实例”BLACK 调用此方法...那么它不会返回 true。

isEnumBlack(BLACK) 将返回 false,即使根据代码它应该返回 true。枚举将不再是枚举!

If you had two enums then the following instruction could be true or false...

public boolean isEnumBlack(EnumType enum) {
    if (enum == EnumType.BLACK) {
        return true;
    } else {
        return false;
    }
}

Then if you called this method with an "instance" of the EnumType, BLACK... then it would not return true.

isEnumBlack(BLACK) would return false, even though according to the code it should be returning true. Enum's would no longer be enum's!

伪心 2024-10-21 10:25:42

除了所有其他好的答案之外,它还会破坏 EnumSetEnumSet 根据枚举的大小实现为单个 longlong 数组,其中每个位代表包含内容或排除一个特定的枚举值。如果枚举的数量和顺序不是恒定的,那么这将不起作用。

In addition to all of the other good answers, it would break EnumSet. EnumSet is implemented, depending on the size of the enum, as a single long or an array of longs, where each bit represents the inclusion or exclusion of one particular enum value. If the number and order of enums were not constant, this would not work.

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