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 枚举类 | 菜鸟教程

相关推荐
Xzaveir6 小时前
别把所有“认证”都塞进 AuthService:实名、一键登录与号码身份的领域拆分
android·人工智能
BerrySen1786 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
YM52e8 小时前
鸿蒙Flutter Center居中组件:Align对齐详解
android·学习·flutter·华为·harmonyos·鸿蒙
时间的拾荒人9 小时前
MySQL 视图详解
android·数据库·mysql
祉猷并茂,雯华若锦9 小时前
Appium 3.x安卓按键与通知栏操作全指南
android·appium
码农coding10 小时前
android 12 SurfaceFlinger开机启动分析
android
hunterandroid10 小时前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端
码农coding10 小时前
android12 开机启动PMS
android
Days205011 小时前
GPT-Image-2 国风美学人设生成提示词分享
android·gpt
浮江雾11 小时前
Flutter第十节-----Flutter布局与组件全解析
android·开发语言·前端·学习·flutter·入门