28 Extra concepts
141
1
enum class Day {
2
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
3
THURSDAY, FRIDAY, SATURDAY
4
}
Enums can have parameters:
1
enum class Icon(val res: Int) {
2
UP(R.drawable.ic_up),
3
SEARCH(R.drawable.ic_search),
4
CAST(R.drawable.ic_cast)
5
}
6
7
val searchIconRes = Icon.SEARCH.res
Enums can be requested by the
String
that matches a name, and we can also get an
Array
which
includes all the values of an enum, so that we can iterate over them.
1
val search: Icon = Icon.valueOf("SEARCH")
2
val iconList: Array
= Icon.values
Besides, every Enum constant has functions to obtain its name and the position in the declaration:
1
val searchName: String = Icon.SEARCH.name
2
val searchPosition: Int = Icon.SEARCH.ordinal
Enums implement
Comparable
based on the constants ordinal, so it’s easy to compare them.
Do'stlaringiz bilan baham: