The enum class with a constructor

Each member of the enum class contains an integer value that starts from 0, but Kotlin also allows us to assign a value of our own choice using a constructor: 

enum class Week(val value: Int) {
MONDAY(2 ), TUESDAY(4), WEDNESDAY(6), THURSDAY(8), FRIDAY(10), SATURDAY(12), SUNDAY(14)
}

The Week enum class has a constructor with one integer property. Each member in the enum class is bound to provide an initial value:

fun main(args: Array<String>) {
val week = Week.valueOf("TUESDAY")
println("Item type: " + week)
println("Name: " + week.name)
println("Value: " + week.value)
}

Verify the output by assigning a different values of the week enum.

The output of the preceding example is as follows:

Item type: TUESDAY, Name: TUESDAY, Value: 4
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.242