欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Item 31: Use instance fields instead of ordinals

程序员文章站 2022-05-18 14:41:15
...

1.  All enums have an ordinal method, which returns the numerical position of each enum constant in its type.

 

2.  Never derive a value associated with an enum from its ordinal; store it in an instance field instead.

 

3.  ordinal is designed for use by general-purpose enum based data structures such as EnumSet and EnumMap. Unless you are writing such a data structure, you are best off avoiding the ordinal method entirely.