Article Directory
- I. Enumeration class common usage
- II. Enumeration class initialization
- III. Class anonymous enumeration class
- IV. Enumeration class that implements the interface
- V. Gets an enumeration of constant name and location of the index
- VI. Gets the member variables enumeration constant
- VII. Enumeration constant method call
- VIII. Kotlin enumeration complete code examples
I. Enumeration class common usage
The most common method of enumeration: enumeration is defined constants, separated by commas, each enumeration constant is an object;
/**
* 枚举最常用的方法
* 定义枚举常量 , 使用逗号隔开
* 每个枚举常量都是一个对象
*
* 这里定义了 左 右 两个方向
*/
enum class Direction{
LEFT, RIGHR
}
II. Enumeration class initialization
Enumeration Initialization: When you define enumeration constants for its members to declare a variable, you can initialize a value for
/**
* 枚举初始化
* 定义枚举常量时 , 为其声明一个成员变量 , 可以为其初始化一个值
*/
enum class Number (var num : Int){
ONE(1),
TWO(2)
}
III. Class anonymous enumeration class
Enumeration class abstract methods: enumeration method abstract class enumeration constants can be defined;
① ways: each enumeration constants must implement the abstract methods defined here in the anonymous class;
② define the location: the abstract methods defined in the enumeration constant to the last enumeration class;
/**
* 枚举常量匿名类
*/
enum class Student{
GOOD_STUDENT{
override fun say() {
println("好学生")
}
},
BAD_STUDENT{
override fun say() {
println("坏学生")
}
}; //定义了成员方法后, 最后一个枚举常量后需要使用分号
/**
* 定义枚举常量的抽象方法
* 每个 枚举常量 必须在 匿名类中 实现此处定义的抽象方法
* 枚举常量的抽象方法要定义在枚举类最后
*/
abstract fun say()
}
IV. Enumeration class that implements the interface
Enumeration class implements the interface: enumeration class can implement an interface, the method is equivalent to the abstract interface defined in the enumeration class;
/**
* 声明一个接口
*/
interface Speak{
fun speak()
}
/**
* 该枚举类实现 human 接口
* 每个枚举常量都要实现 speak 方法
*/
enum class Human : Speak{
MAN{
override fun speak() {
println("男人")
}
},
WOMEN{
override fun speak() {
println("女人")
}
}
}
V. Gets an enumeration of constant name and location of the index
Gets an enumeration of constant name and location of the index:
① Gets an enumeration class name: 枚举类.常量名称.name
you can get the name of the enumeration constant;
(Printing 枚举类.常量名称
can also print out the name of the enumeration constants)
② obtaining enumeration class index: 枚举类.常量名称.ordinal
can obtain the enumeration constant position index, counting from zero;
//打印枚举类 , 直接打印其名称
//LEFT
println(Direction.LEFT)
//RIGHR
println(Direction.RIGHR)
//ONE
println(Number.ONE)
//TWO
println(Number.TWO)
//打印枚举类 name 名称
//LEFT
println(Direction.LEFT.name)
//RIGHR
println(Direction.RIGHR.name)
//ONE
println(Number.ONE.name)
//TWO
println(Number.TWO.name)
//打印枚举类 ordinal 位置索引
//0
println(Direction.LEFT.ordinal)
//1
println(Direction.RIGHR.ordinal)
VI. Gets the member variables enumeration constant
When enumerating definitions, it can formulate its member variables enum class 枚举名称 : ( var 成员名 : 成员类型 )
, enumeration constants defined, you can initialize its value 枚举常量名称 ( 成员值 )
;
//打印枚举类 成员变量值
//1
println(Number.ONE.num)
//2
println(Number.TWO.num)
VII. Enumeration constant method call
Use 枚举类.枚举常量名.方法名()
can call an enumeration method enumeration constants anonymous class;
//调用枚举类的实现的自身定义的抽象方法
//好学生
Student.GOOD_STUDENT.say()
//坏学生
Student.BAD_STUDENT.say()
//调用枚举类实现的接口方法
//男人
Human.MAN.speak()
//女人
Human.WOMEN.speak()
VIII. Kotlin enumeration complete code examples
package enum
/**
* 枚举最常用的方法
* 定义枚举常量 , 使用逗号隔开
* 每个枚举常量都是一个对象
*
* 这里定义了 左 右 两个方向
*/
enum class Direction{
LEFT, RIGHR
}
/**
* 枚举初始化
* 定义枚举常量时 , 为其声明一个成员变量 , 可以为其初始化一个值
*/
enum class Number (var num : Int){
ONE(1),
TWO(2)
}
/**
* 枚举常量匿名类
*/
enum class Student{
GOOD_STUDENT{
override fun say() {
println("好学生")
}
},
BAD_STUDENT{
override fun say() {
println("坏学生")
}
}; //定义了成员方法后, 最后一个枚举常量后需要使用分号
/**
* 定义枚举常量的抽象方法
* 每个 枚举常量 必须在 匿名类中 实现此处定义的抽象方法
* 枚举常量的抽象方法要定义在枚举类最后
*/
abstract fun say()
}
/**
* 声明一个接口
*/
interface Speak{
fun speak()
}
/**
* 该枚举类实现 human 接口
* 每个枚举常量都要实现 speak 方法
*/
enum class Human : Speak{
MAN{
override fun speak() {
println("男人")
}
},
WOMEN{
override fun speak() {
println("女人")
}
}
}
fun main() {
//打印枚举类 , 直接打印其名称
//LEFT
println(Direction.LEFT)
//RIGHR
println(Direction.RIGHR)
//ONE
println(Number.ONE)
//TWO
println(Number.TWO)
//打印枚举类 name 名称
//LEFT
println(Direction.LEFT.name)
//RIGHR
println(Direction.RIGHR.name)
//ONE
println(Number.ONE.name)
//TWO
println(Number.TWO.name)
//打印枚举类 ordinal 位置索引
//0
println(Direction.LEFT.ordinal)
//1
println(Direction.RIGHR.ordinal)
//打印枚举类 成员变量值
//1
println(Number.ONE.num)
//2
println(Number.TWO.num)
//调用枚举类的实现的自身定义的抽象方法
//好学生
Student.GOOD_STUDENT.say()
//坏学生
Student.BAD_STUDENT.say()
//调用枚举类实现的接口方法
//男人
Human.MAN.speak()
//女人
Human.WOMEN.speak()
}
Results of the :
LEFT
RIGHR
ONE
TWO
LEFT
RIGHR
ONE
TWO
0
1
1
2
好学生
坏学生
男人
女人