android kotlin基础复习 enum

1、kotlin中,关键字enum来定义枚举类型。枚举类型可以包含多个枚举常量,并且每个枚举常量可以有自己的属性和方法。

2、测试代码:

复制代码
enum class Color{
    RED,YELLOW,BLACK,GOLD,BLUE,GREEN,WHITE
}

inline fun <reified T : Enum<T>> printAllValues() {
    print(enumValues<T>().joinToString { it.name })
}

fun main(args: Array<String>) {
    var color:Color=Color.GOLD

    println(Color.entries.toTypedArray())
    println(Color.valueOf("RED"))
    println("----------------------------")
    println(color.name)
    println(color.ordinal)
    println("----------------------------")
    printAllValues<Color>()
}

3、输出:

复制代码
[Lcom.xxx.myapplication.test.Color;@1a407d53
RED
----------------------------
GOLD
3
----------------------------
RED, YELLOW, BLACK, GOLD, BLUE, GREEN, WHITE
Process finished with exit code 0

4、说明:

color.name:输出值。

color.ordinal:输出索引。

enumValues<T>().joinToString { it.name }:将name,组合起来以逗号隔开输出。

RED, YELLOW, BLACK, GOLD, BLUE, GREEN, WHITE

复制代码
println(Color.entries); 输出上面的数组。

参考:Kotlin 枚举类 | 菜鸟教程

相关推荐
游戏开发爱好者84 分钟前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者81 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
wxson728216 小时前
【Android视频监控系统技术总结】
android·音视频
hunterandroid18 小时前
[Android 从零到一] LiveData 粘性事件与单次消费:从重复触发到可靠事件总线
android
hunterandroid18 小时前
[Android 从零到一] Android 事件分发机制:从 ACTION_DOWN 到 View 事件消费链路
android
阿巴斯甜18 小时前
adb‑shell 全套命令实操总结
android
奔跑的架构师19 小时前
[A-49]ARMv9/v8-PSCI接口规范与工作流程简介
android·linux·arm开发·arm
AFinalStone20 小时前
Android 7系统国际化(七)Resources 与 AssetManager 源码剖析
android·国际化·local
Android-Flutter20 小时前
Android 内存泄漏详解
android·kotlin