티스토리 뷰

공부

[kotlin] `sealed class` vs `enum class`

승가비 2022. 8. 6. 06:24
728x90

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.

 

https://stackoverflow.com/questions/49169086/sealed-class-vs-enum-when-using-associated-type

 

sealed class vs enum when using associated type

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 is better than the other. Using sealed class: sealed class

stackoverflow.com

 

728x90
댓글