How can I create static m...
How can I create static m...
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"
}
}
}
}
In the enum class, define a companion object. This is where you can add your static methods.
Inside the companion object, define your static method(s). In this example, we have a getColorName
function that t...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào