LoginSignup
5
7

More than 5 years have passed since last update.

[Kotlin] enumに付加情報を付け加える

Posted at

例えば Int型のvalueString型のmessage の2つを加える場合は次のとおり。

enum class Code(val value: Int, val message: String) {
    CODE1(100, "message hoge"),
    CODE2(200, "message fuga"),
    CODE3(300, "message piyo"),
}

次のように利用できます。

print(Code.CODE1.value)   // 100 が出力される
print(Code.CODE1.message) // message hoge が出力される
5
7
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
5
7