Kotlin Enum Class
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Enums are named constants in it's most basic usage. We can declare them using the enum followed by class keywords. Enums in Kotlin can also perform complex operations like implementing interfaces etc.
Sample enum class in Kotlin
In following example we are creating an enum class DayOfWeek to represent the 7 days of the week.
enum class DayOfWeek {
SUNDAY, MONDAY, TUESDAY,
WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY
}
We can use enum DayOfWeek as follows.In following example, we are creating a variable to represent Sunday, and printing it to the console.
fun main(args: Array<String>)
{
var daynow = DayOfWeek.SUNDAY
println(daynow)
}
Output:
SUNDAY
We can also initialize enums to represent a number for example. We can then access the underlying value using value member variable under the enum. We tried this below by initializing the days of the week with numbers and then accessing the underlying value for Thursday.
Example:
enum class DayOfWeek(val value: Int){
Sunday(0),
Monday(1),
Tuesday(2),
Wednesday(3),
Thursday(4),
Friday(5),
Saturday(6)
}
fun main(args: Array<String>)
{
var daynow = DayOfWeek.Thursday
println(daynow)
println(daynow.value)
}
Output:
Thursday
4
In the following example we are iterating through the complete values of enums. We can do that using values function.
enum class DayOfWeek(val value: Int){
Sunday(0),
Monday(1),
Tuesday(2),
Wednesday(3),
Thursday(4),
Friday(5),
Saturday(6)
}
fun main(args: <String>)
{
for(day in DayOfWeek.values())
{
println(day)
}
}
Output:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday