introduction
Translated from:/mastering-e…
As a modern and powerful programming language, Kotlin can provide developers with many features and tools to help us write more efficient and readable code.
One of the important characteristics isEnum
Enumeration, essentially a data type: allows you to define a set of constants distinguished by names.
This article will use code cases to explore the advanced usage of Kotlin enumeration, and thus help everyone understand how to use it.Enum
Better apply to the project.
1. Enumeration class
It can be said that Enum Classes is an excellent way to show a set of constants in Kotlin.
Specifically, it allows you to define a limited number of members to qualify data types, and you can easily use these enum types everywhere in your code.
As follows, we use the enum keyword to define the enum types for each day of the week.
enum class DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
Then use the enum freely in the code, for example:
fun getWeekendDays(): List<DayOfWeek> { return listOf(, ) }
2. Enumerate properties
In addition to presentation types, Kotlin Enum can also have property properties, which means developers can add additional information to enum members.
For example, below, we giveDayOfWeek
The enumeration adds the serial number attributes for each day within the week.
enum class DayOfWeek(val number: Int) { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7) }
Then you can obtain the serial number information for that day.
fun getDayNumber(day: DayOfWeek): Int { return }
3. Enumerate functions
Kotlin Enum also supports defining functions, so functional methods can be defined internally for external use.
As followsDayOfWeek
Add an isWeekend() function to determine whether the day belongs to the weekend.
enum class DayOfWeek(val number: Int) { MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6), SUNDAY(7); fun isWeekend(): Boolean { return this == SATURDAY || this == SUNDAY } }
Where this enumeration is used, you can directly use this function to make judgments.
fun printDayType(day: DayOfWeek) { if (()) { println("$day is a weekend day.") } else { println("$day is a weekday.") } }
4. Enumeration constructor
Since Enum can have properties, constructors are naturally supported, so developers can add sufficient information when constructing instances.
For example, we areDayOfWeek
In the enumeration constructor, add the name information of the day beyond the sequence number.
enum class DayOfWeek(val number: Int, val displayName: String) { MONDAY(1, "Monday"), TUESDAY(2, "Tuesday"), WEDNESDAY(3, "Wednesday"), THURSDAY(4, "Thursday"), FRIDAY(5, "Friday"), SATURDAY(6, "Saturday"), SUNDAY(7, "Sunday"); override fun toString(): String { return displayName } }
This allows you to obtain the name data carried by the enumeration.
fun printDayName(day: DayOfWeek) { println("The day of the week is ${}") }
5. Enumerate extension functions
Like ordinary classes, you can also add extension functions for Enum Class. We can add additional functional functions as needed outside the enumeration class.
For example, hereDayOfWeek
Enumeration extends a function that gets the next day.
fun (): DayOfWeek { return when (this) { MONDAY -> TUESDAY TUESDAY -> WEDNESDAY WEDNESDAY -> THURSDAY THURSDAY -> FRIDAY FRIDAY -> SATURDAY SATURDAY -> SUNDAY SUNDAY -> MONDAY } }
Use this extension function freely like calling a function defined by the enum itself.
fun printNextDay(day: DayOfWeek) { println("The next day is ${()}") }
Conclusion
You can see that Kotlin Enum can help developers define a set of types of constants: greatly simplify the code, better readability, and provide additional functional functions.
Through the above advanced usage, I believe that everyone can use Enum to create more robust and efficient code, which is also easier to understand and maintain.
The above is the detailed content of in-depth study of the concise and efficient advanced usage of Kotlin enumeration. For more information about Kotlin enumeration, please pay attention to my other related articles!