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

相关推荐
Zeaon42 分钟前
别再手写筛选栏了:一个可组合的 Flutter 选择组件(5 入口 × 7 委托)
android·flutter
Michaelwubo1 小时前
mysql8 主从 HA切换
android·adb
墨狂之逸才1 小时前
Android 工业手持机蓝牙扫描失败:为什么同时需要蓝牙和定位权限
android
Android打工仔2 小时前
Kotlin 协程源码解析(七)`delay()` 到底是如何在 `Dispatchers.Main` 上恢复的?
android·kotlin
律宏阔2 小时前
Android 红外功能实现与红外协议开发笔记:ConsumerIrManager、NEC、PWM、曼彻斯特编码
android
mmsx3 小时前
MapLibre 统一图层模型:sealed class + 声明式图层 + XML 持久化
android·源码·地图·maplibre
YUJIANYUE3 小时前
查立得万用查分安卓版(web环境+查询系统免安装单文件一键运行包)
android·前端
天空之城--4 小时前
Android Launcher 性能优化完全指南:从启动到滑动,构建极致流畅的桌面体验
android·性能优化
hunterandroid4 小时前
Android ANR 排查实战:从线上告警到主线程卡点定位
android·前端
TimeFine4 小时前
智能眼镜开发:图片翻译与EXIF的重要性
android