There's something new in the enumeration class in Kotlin: The constant of an enumeration class can be seen as an anonymous class with the same name.
Since it is a class, it can be associated with a method
Check the code of the official website
If you have experienced the use of enumerations in other languages, you may be confused by this definition and explanation.
I'll give you an example and a result, and you'll be much clearer.
Enum class Protocolstate {
Waiting {
Override Fun signal () = talking
},
Talking {
Override Fun signal () = waiting
};
Abstract Fun Signal (): protocolstate
}
Fun Main (args:array<string>) {
Val ev:protocolstate = protocolstate.waiting
println (Ev.signal ())
}
Results
Talking
Finally:
Enumeration constants Support enumeration classes, support methods for overloading parent classes, enumeration constants, anonymous class rules, and constants, but constant anonymous classes and other components of the parent class are used ";" Separated
Well, maybe, you don't like the idea.
However, you may fall in love with Kotlin when you use it for practical development, after all, any innovation is definitely an implicit demand from the old road.
If you don't, it's likely that you're not daoxing enough.
Kotlin Enum Class Anonymous class instance