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

相关推荐
爱笑鱼5 分钟前
Android 系统启动机制(二):init.rc 不是普通脚本,service、action 和 property 怎样驱动启动?
android
新鲜势力呀21 分钟前
深入理解 Elliot CUDA 编程核心:PHP 实现 CUDA 并行计算思想与实践
android·开发语言·php
恋猫de小郭1 小时前
Flutter 3.47 首坑,analysis_options 问题连环回归
android·前端·flutter
ue星空3 小时前
第一个安卓程序
android
淡淡的香烟3 小时前
Androidiot蓝牙配网简单封装
android·物联网
gxgldyh3 小时前
Android Framework源码解析(九):App进程诞生全流程——从AMS请求到Zygote fork源码深度拆解
android·framework·zygote·android 启动流程·android app创建流程
牛哇网络工作室10 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
一笑的小酒馆11 小时前
Androidiot蓝牙配网简单封装
android
愚公搬代码13 小时前
【愚公系列】《Android应用案例开发大全》016-LBS类应用掌上杭州(辅助工具类的开发)
android·前端
g105655913914 小时前
LAMP博客平台Wordpress实战
android·mysql·nginx·php