Why use Enums instead of Constants? Which is better in terms of software design and readability

后端 未结 5 909
一个人的身影
一个人的身影 2020-11-28 04:21

I have a scenario in which I have Player types ARCHER,WARRIOR, and sorcerer.
What should I use in Player class fo

5条回答
  •  独厮守ぢ
    2020-11-28 04:58

    Enum is better to use for type safety. Wrong values cannot be entered. But enum in android takes so much memory, you should use intdef instead. Refer to this answer for Example and explanation:-

    IntDef/StringDef Example

    You can also check android source code it is replacing enums with IntDef/StringDef wherever possible. Ex. View.VISIBLE.

提交回复
热议问题