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 訊息


留言

熱門文章

Google Play Console 2022 上架資訊,審核通過時間

2021申請 Android Developer 開發者帳號及上架步驟

如何創造出難以維護的程式碼