sealed-class

sealed class vs enum when using associated type

*爱你&永不变心* 提交于 2020-04-29 08:58:22
问题 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 SealedColor(val value: Int) { class Red : SealedColor(0) class Green : SealedColor(1) class Blue : SealedColor(2) companion object { val map = hashMapOf( 0 to Red(), 1 to Green(), 2 to Blue() ) } } val sealedColor: SealedColor = SealedColor.map[0]!! when (sealedColor) { is SealedColor.Red -> print(