简体   繁体   English

Java:枚举和值类之间的区别(也在使用中)是什么?

[英]Java: What is the difference (also in use) between an enumeration and a value class?

I fail to see the difference between a value class and an enumeration. 我没有看到值类和枚举之间的区别。 It would be great if someone could tell me the difference between them and what the difference in their use is. 如果有人可以告诉我它们之间的区别以及它们的使用差异,那将会很棒。

First of all, it is not a "value class". 首先,它不是“价值等级”。 It is a "value-based class". 这是一个“基于价值的阶级”。 Please try to use the correct terminology. 请尝试使用正确的术语。

The "Value-based Classes" blog entry by Nicolai Parlog explains in considerable detail what these are all about. Nicolai Parlog的“基于价值观的课程”博客文章详细解释了这些内容的全部内容。 In essence, they are a predecessor of "value types" that are likely to be added in a future version of Java. 实质上,它们是“值类型”的前身,可能会在未来的Java版本中添加。 (Probably Java 10 - see Project Valhalla .) (可能是Java 10 - 参见Valhalla项目 。)

The main reason that enum types (in general) are not value-based classes is that they are not necessarily immutable. enum类型(通常)不是基于值的类的主要原因是它们不一定是不可变的。 Immutability is one of the key requirements for a class to be value-based. 不可变性是课程以价值为基础的关键要求之一。 (There are other aspects of enums that don't quite match up with the requirements, but the special nature of enum values probably mean that these would not matter.) (枚举的其他方面与要求不完全匹配,但枚举值的特殊性可能意味着这些并不重要。)

Right now, the "value-based class" characteristic has no direct use. 目前,“基于价值的阶级”特征没有直接用途。 But it may do in a future version of Java. 但它可能会在未来的Java版本中发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM