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()
}
相关推荐
道不尽世间的沧桑4 分钟前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
xvch10 分钟前
Kotlin 2.1.0 入门教程(二十五)类型擦除
android·kotlin
久绊A12 分钟前
Python 基本语法的详细解释
开发语言·windows·python
软件黑马王子4 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫4 小时前
go orm GORM
开发语言·后端·golang
李白同学5 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?6 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农7 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿7 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风7 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言