sealed class vs enum when using associated type

前端 未结 2 945
一个人的身影
一个人的身影 2020-12-31 00:44

I\'d like to create a color object based on an Int. I can achieve the same result using sealed class and enum and was wondering if one

2条回答
  •  無奈伤痛
    2020-12-31 01:39

    A sealed class is "an extension of enum classes". They can exist in multiple instances that contain state while each enum constant exists only as a single instance.

    Since, in your example, you don't need the values to be instantiated multiple times and they don't provide special behavior, enums should just be right for the use case.

    Also, see the docs.

提交回复
热议问题