How can I create static method for enum in Kotiln?
How can I create static method for enum in Kotiln?
In Kotlin, you can create static methods for enums using companion objects. Here's how you can do it:
enum class Color {
RED, GREEN, BLUE;
companion object {
fun getColorName(color: Color): String {
return when (color) {
RED -> "Red"
GREEN -> "Green"
BLUE -> "Blue"
}
}
}
}
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.