列舉

Prerequisites:

什麼是列舉?請詳見Java的列舉。

宣告語法

在kotlin宣告enum,跟Java略有不同,是用enum class 開頭。

enum class 類別名 {

}

主要建構式

在Kotlin,主要建構式不能使用name這個變數名。

1
2
3
4
enum class Gender(val title : String) {
    GIRL("女孩"),
    BOY("男孩");
}

使用方法

1
2
3
4
fun main() {
    println(Gender.BOY.title)
    println(Gender.GIRL.title)
}
男孩
女孩

沒有主要建構式

1
2
3
4
enum class Gender {
    GIRL,
    BOY;
}

印出Enum的靜態屬性,預設印出屬性名,這部分跟Java是相同的。

1
2
3
4
fun main() {
    println(Gender.BOY)
    println(Gender.GIRL)
}
BOY
GIRL

results matching ""

    No results matching ""