An enumeration class is a set of named constants, which means that the values of the variables are listed one by one, and the values of the variables are limited to the range of the listed values.
The most basic usage of enumeration class is to implement type-safe enumeration:
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
Each enum constant is an object. Enumeration constants are separated by commas.
How to use enumeration class?
println(Direction.EAST.name)
打印结果:
EAST
Use .name to get the string of the enum constant itself.
Each enumeration constant has attributes to obtain its name and location in the enumeration class declaration:
val name: String
val ordinal: Int
That is to say, the constants in each enumeration class can be named by .name and position by .ordinal .
==========================================================================
How to use Int, boolean and other types for enumeration? You can initialize the enum class with parameters
enum class Season(val temperature: Int) {
SPRING(1),
SUMMER(2),
AUTUMN(3),
WINTER(4),
}
println("name: ${Season.SPRING.name} , temperature: ${Season.SPRING.temperature}")
打印结果:
name: SPRING , temperature: 1
enum class Week(val index: Int, val aliasName: String) {
MONDAY(1, "Mon"),
TUESDAY(2, "Tue"),
WEDNESDAY(3, "Wed"),
THURSDAY(4, "Thu"),
FRIDAY(5, "Fri"),
SATURDAY(6, "Sat"),
SUNDAY(7, "Sun")
}
println("${Week.MONDAY.name} : " +
" index ${Week.MONDAY.index} , aliasName: ${Week.MONDAY.aliasName}")
打印结果:
MONDAY : index 1 , aliasName: Mon
==========================================================================
Enumeration classes can implement interfaces
interface MyInterface1 {
fun myMethod1(num: Int)
}
interface MyInterface2 {
fun myMethod2(num: Int)
}
enum class MyEnumClass : MyInterface1, MyInterface2 {
OPERATE1 {
override fun myMethod2() {
println("OPERATE1 对 myMethod2 的特殊实现 ")
}
},
OPERATE2 {
override fun myMethod2() {
println("OPERATE2 对 myMethod2 的特殊实现 ")
}
};
override fun myMethod1() {
println("MyEnumClass 对 myMethod1 的统一实现 ")
}
}
主函数
fun main() {
MyEnumClass.OPERATE1.myMethod1()
MyEnumClass.OPERATE2.myMethod1()
MyEnumClass.OPERATE1.myMethod2()
MyEnumClass.OPERATE2.myMethod2()
}
打印结果:
D:\AndroidStudio\android-studio\jre\bin\java.exe
MyEnumClass 对 myMethod1 的统一实现
MyEnumClass 对 myMethod1 的统一实现
OPERATE1 对 myMethod2 的特殊实现
OPERATE2 对 myMethod2 的特殊实现
Process finished with exit code 0
myMethod1 is implemented uniformly in MyEnumClass, OPERATE1 and OPERATE2 are not implemented by themselves, but they can also be called.
myMethod2 is handled differently in OPERATE1 and OPERATE2.
So if myMethod1 has a unified implementation in MyEnumClass, can OPERATE1 or OPERATE2 also overload myMethod1?
The answer is yes, it will execute its own special implementation when calling, instead of the unified implementation in MyEnumClass
interface MyInterface1 {
fun myMethod1(num: Int)
}
interface MyInterface2 {
fun myMethod2(num: Int)
}
enum class MyEnumClass : MyInterface1, MyInterface2 {
OPERATE1 {
override fun myMethod2() {
println("OPERATE1 对 myMethod2 的特殊实现 ")
}
},
OPERATE2 {
这里也实现了 myMethod1
override fun myMethod1() {
println("OPERATE2 对 myMethod1 的特殊实现 ")
}
override fun myMethod2() {
println("OPERATE2 对 myMethod2 的特殊实现 ")
}
};
override fun myMethod1() {
println("MyEnumClass 对 myMethod1 的统一实现 ")
}
}
主函数
fun main() {
MyEnumClass.OPERATE1.myMethod1()
MyEnumClass.OPERATE2.myMethod1()
MyEnumClass.OPERATE1.myMethod2()
MyEnumClass.OPERATE2.myMethod2()
}
打印结果:
D:\AndroidStudio\android-studio\jre\bin\java.exe
MyEnumClass 对 myMethod1 的统一实现
OPERATE2 对 myMethod1 的特殊实现
OPERATE1 对 myMethod2 的特殊实现
OPERATE2 对 myMethod2 的特殊实现
Process finished with exit code 0
When OPERATE2 calls myMethod1, the print result becomes: OPERATE2's special implementation of myMethod1
==========================================================================