enum class:-
- Enum constants can have properties, methods etc
- Each object of enum class act as separate instance of the class and is separated by commas.
- It increase the readability of code by assigning pre-defined names to constants.
- An instance of an enum class cannot be created using constructors.
class MyKotlin {
fun main() {
println(MyEnum.MONDAY)
for (i in MyEnum.entries) {
println(i)
}
}
enum class MyEnum(i: Int, j: Int) {
SUNDAY(1, 3),
MONDAY(2, 4),
TUESDAY(3, 5),
WEDNESDAY(4, 6),
THRUSDAY(5, 7),
FRIDAY(6, 8),
SATURDAY(7, 9)
}
}
No comments:
Post a Comment