我可以在Java中为枚举指定序数吗? [英] Can I specify ordinal for enum in Java?

查看:181
本文介绍了我可以在Java中为枚举指定序数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ordinal()方法返回枚举实例的序数。

如何设置枚举的序数?

The ordinal() method returns the ordinal of an enum instance.
How can I set the ordinal for an enum?

推荐答案

您无法设置。它始终是不变定义的序数。请参阅有关Enum.ordinal的文档()

You can't set it. It is always the ordinal of the constant definition. See the documentation for Enum.ordinal():


返回此枚举常量的序数(其在其枚举声明中的位置,其中分配了初始常量零的序数)。大多数程序员将不会使用这种方法。它被设计为使用复杂的基于枚举的数据结构,如EnumSet和EnumMap。

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.

其实 - 你不应该需要。如果你想要一些整数属性,定义一个。

And actually - you should not need to. If you want some integer property, define one.

这篇关于我可以在Java中为枚举指定序数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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