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

后端 未结 5 921
一个人的身影
一个人的身影 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 05:04

    I would advice you to use enums only if you really need enumerated constants, or some additional functionality common for all items.

    That's of course depending on the type of application you are writing and what versions and devices you want to support.

    The reason is, enums add overhead because they allocate instances of their items. You can notice, that there are minimum enums in android platform, and almost all constants are final static ints (like View.GONE and stuff like that)

提交回复
热议问题