Kotlin:1.9.0 的新特性

一、概述

Kotlin 1.9.0版本英语官方文档

Kotlin 1.9.0 中文官方文档

The Kotlin 1.9.0 release is out and the K2 compiler for the JVM is now in Beta. Additionally, here are some of the main highlights:

Kotlin 1.9.0版本已经发布,用于JVM的K2编译器现在处于Beta阶段。此外,以下是一些主要亮点:

主要演示的其中四个特性,测试结果图如下

二 、稳定版枚举类的 使用 entries 替代 values 函数

bash 复制代码
enum class Color(val colorName: String, var rgb: String) {
    RED("红色", "#FF0000"),
    ORANGE("橘色", "#FF7F00"),
    YELLOW("Yellow", "#FFFF00")
}

// https://book.kotlincn.net/text/whatsnew1820.html
// entries 属性是在 Kotlin 1.8.20 中作为实验性功能引入的,在 Kotlin 1.9.0 中已经稳定

// 稳定版枚举类的 使用 entries 替代 values 函数
fun findColoBy(rbg: String?): Color? = Color.entries.find { it.rgb == rbg }

enum class RGB { RED, GREEN, BLUE }

/**
 *  稳定版枚举类的 使用 entries 替代 values 函数
 */
fun testEnum() {
    //    for (color in RGB.values()) println(color.toString())
    for (color in RGB.entries) println(color.toString())
    //RED
    //GREEN
    //BLUE

    println("----------------------------------- end")
}

三、稳定版用于前闭后开区间的 ...< 操作符

bash 复制代码
fun testOpenIntervalWithFrontClosed() {

//    for (number in 2 until 6) {
//        println("number = $number")
//    }
    //前闭后开区间的 ..< 操作符
    for (number in 2..<6) {
        println("number = $number")
        //number = 2
        //number = 3
        //number = 4
        //number = 5
    }

    println("-------------------- 稳定版用于前闭后开区间的 ...< 操作符 ------- end")
}

四、新增按名称获取正则表达式捕获组的公共函数

bash 复制代码
/**
 * 新增按名称获取正则表达式捕获组的公共函数
 */

fun testRegex() {
    val regex =
        """\b(?<city>[A-Za-z\s]+),\s(?<state>[A-Z]{2}):\s(?<areaCode>[0-9]{3})\b""".toRegex()
    val input = "Coordinates: Austin, TX: 123"
    // 坐标:奥斯汀,德克萨斯州:123

    val match = regex.find(input)!!
    println(match.groups["city"]?.value)
    // Austin
    println(match.groups["state"]?.value)
    // TX
    println(match.groups["areaCode"]?.value)
    // 123

    println("-------------------- 新增按名称获取正则表达式捕获组的公共函数 ------- end")
}

五、数据类相称的数据对象

bash 复制代码
//数据类相称的数据对象
sealed interface ReadResult
data class Number(val number: Int) : ReadResult
data class Text(val text: String) : ReadResult

data object EndOfFile : ReadResult

/**
 * 数据类相称的数据对象
 */
fun testDataObject(){
    println(Number(7))
    println(EndOfFile)
    //Number(number=7)
    //EndOfFile
    println("-------------------- 数据类相称的数据对象 ------ end")
}

六、kt_190文件完整代码

bash 复制代码
package com.example.test.ktversion

/*
    https://book.kotlincn.net/text/whatsnew19.html
 */

enum class Color(val colorName: String, var rgb: String) {
    RED("红色", "#FF0000"),
    ORANGE("橘色", "#FF7F00"),
    YELLOW("Yellow", "#FFFF00")
}

// https://book.kotlincn.net/text/whatsnew1820.html
// entries 属性是在 Kotlin 1.8.20 中作为实验性功能引入的,在 Kotlin 1.9.0 中已经稳定

// 稳定版枚举类的 使用 entries 替代 values 函数
fun findColoBy(rbg: String?): Color? = Color.entries.find { it.rgb == rbg }

enum class RGB { RED, GREEN, BLUE }


//数据类相称的数据对象
sealed interface ReadResult
data class Number(val number: Int) : ReadResult
data class Text(val text: String) : ReadResult

data object EndOfFile : ReadResult

/**
 *  稳定版枚举类的 使用 entries 替代 values 函数
 */
fun testEnum() {
    //    for (color in RGB.values()) println(color.toString())
    for (color in RGB.entries) println(color.toString())
    //RED
    //GREEN
    //BLUE

    println("-------------------  稳定版枚举类的 使用 entries 替代 values 函数 ----- end")
}

/**
 * 稳定版用于前闭后开区间的 ...< 操作符
 */
fun testOpenIntervalWithFrontClosed() {

//    for (number in 2 until 6) {
//        println("number = $number")
//    }
    //前闭后开区间的 ..< 操作符
    for (number in 2..<6) {
        println("number = $number")
        //number = 2
        //number = 3
        //number = 4
        //number = 5
    }

    println("-------------------- 稳定版用于前闭后开区间的 ...< 操作符 ------- end")
}

/**
 * 新增按名称获取正则表达式捕获组的公共函数
 */

fun testRegex() {
    val regex =
        """\b(?<city>[A-Za-z\s]+),\s(?<state>[A-Z]{2}):\s(?<areaCode>[0-9]{3})\b""".toRegex()
    val input = "Coordinates: Austin, TX: 123"
    // 坐标:奥斯汀,德克萨斯州:123

    val match = regex.find(input)!!
    println(match.groups["city"]?.value)
    // Austin
    println(match.groups["state"]?.value)
    // TX
    println(match.groups["areaCode"]?.value)
    // 123

    println("-------------------- 新增按名称获取正则表达式捕获组的公共函数 ------- end")
}

/**
 * 数据类相称的数据对象
 */
fun testDataObject(){
    println(Number(7))
    println(EndOfFile)
    //Number(number=7)
    //EndOfFile
    println("-------------------- 数据类相称的数据对象 ------ end")
}

fun main() {
    testEnum()
    testOpenIntervalWithFrontClosed()
    testRegex()
    testDataObject()
}
相关推荐
用户20187928316710 分钟前
Binder 同应用内(本地)通信是否存在 1MB 大小限制?
android
柯南二号19 分钟前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
一条上岸小咸鱼25 分钟前
Kotlin 基本数据类型(四):String
android·前端·kotlin
我是哈哈hh39 分钟前
【Node.js】ECMAScript标准 以及 npm安装
开发语言·前端·javascript·node.js
Onion_991 小时前
学习下Github上的Android CICD吧
android·github
来来走走2 小时前
Flutter Form组件的基本使用
android·flutter
Sammyyyyy2 小时前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js
顾林海2 小时前
Android MMKV 深度解析:原理、实践与源码剖析
android·面试·源码阅读
William一直在路上3 小时前
Python数据类型转换详解:从基础到实践
开发语言·python
雨白3 小时前
TCP/IP 核心概念详解:从网络分层到连接管理
android