Item 31: Use instance fields instead of ordinals

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.

 

你可能感兴趣的:(enum,enumMap,enumset)