2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KotlinでEnumを使う

Last updated at Posted at 2017-07-26

● 概要

  • AndroidJavaと同じようにEnumを使う方法を調査。
  • Enumに複数個の引数を持った場合の動作確認を実施。

◆ Enumの利用方法

  • 基本的なEnumの利用方法は以下参照。

◆ 複数個の引数を持ったEnumの利用方法

  • 記事が見つからなかったので、これで合ってるだろうという感じで以下の様に実装してみました。※動くことは確認してますw
TestEnum.kt
/**
 * numberとnameを持ったEnum
 */
enum class TestEnum(val number: Int,
                    val nameText: String) {
    AAA(2, "aaa"),
    BBB(5, "bbb")
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // 呼び出し
        println("getEnumNameText:" + getEnumNameText(5))
    }
}


/**
 * 引数numberに紐づくTestEnumのNameTextを取得する
 * @param number
 */
fun getEnumNameText(number: Int): String {
    return TestEnum.values().filter { number == it.number }.first().nameText
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?