Kotlin Enum 例子

enum class ToyBrand(val value: Int) {

MATTEL(0),
HASBRO(1),
Lego(2),
BANDAI(3),
Custom(5);

companion object {
fun getTotalSize() = values().size
}
}
create Enum clas
  • 定義 ToyBrand 為 Custom
val brand = ToyBrand.Custom

Log.d("TAG", "value name: " + brand.name)
Log.d("TAG", "value value: " + brand.value)
Log.d("TAG", "value ordinal: " + brand.ordinal)
Debug 訊息

  • 列表
var array: Array<ToyBrand> = ToyBrand.values()
for (item in array) {
Log.d("TAG", "position: ${item.ordinal}, name: ${item.toString()}")
}
Debug 訊息

  • Total Size & valueOf 確認資料型態
Log.d("TAG", "valueOf input String: " + ToyBrand.valueOf(brand.toString()))

Log.d("TAG", "ToyBrand total size: " + ToyBrand.getTotalSize())
Debug 訊息


留言

熱門文章

TourCard 開通全紀錄 台胞證 支付寶

封神榜國際服 符石系統