LoginSignup
2
0

More than 3 years have passed since last update.

【Kotlin】Enumに複数のパラメータを持たせる

Last updated at Posted at 2019-08-16

コード

enum classの定義

enum class Family(val firstName: String, val birthplace: String, val age: Int) {
    TARO("Taro", "tokyo", 30),
    HANAKO("Hanako", "hokkaido", 25)
}
  • valとしてパラメータを定義する
  • nameというパラメータ名は使えない(スーパークラスであるEnumのメンバーとして定義されているため)

呼び出し

val name = Family.TARO.firstName // "Taro"
val age = Family.HANAKO.age // 25
val birthplace = Family.HANAKO.birthplace // "hokkaido"

参考

Kotlin enum with multiple “parameter” - Stack Overflow

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0