syntax:
enum class Name{
CONSTANT1, CONSTANT2, CONSTANT3
}
enum - This is a constant but an object as well. The regular class is preceded by an enum keyword. It is a class. So, each constant is the object created from that enum class.
================
fun main(){
val date = Date.from(Instant.Now())
val dayOfWeek = date.toString().substring(0, 3)
val activityToday = when(dayOfWeek){
"Mon", "Tue" -> MySchedule.JAVASCRIPT
"Wed", "Thu" -> MySchedule.KOTLIN
"Fri", "Sat" -> MySchedule.JAVA
else -> MySchedule.NO_SCHEDULE
}
println("Today: $dayOfWeek
Activity: $activityToday
Message: ${activityToday.message}")
}
enum class MySchedule(val message: String){
JAVASCRIPT("For web development"),
KOTLIN("For android development"),
JAVA("For spring boot in web development"),
NO_SCHEDULE("Free time")
}
================
Result:
Today: Wed
Activity: KOTLIN
Message: For Android development
Note:
1. Since enum is a class, it can declare members like properties and methods though you need to separate the constants you declared to the members using a semi colon at the last constant.
2. Enum can declared an abstract method too. You need to implement this abstract method to each constant you declared in the class.
3. Enum can extend an interface. Of course, you have to override the abstract method of the interface in each of your constant.
4. Just like other classes , enum class has useful built-in methods too like values() which returns an array of object constants of your enum class.
YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
