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 分钟前
数据结构笔试核心考点
java·开发语言·算法
寻找华年的锦瑟32 分钟前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
HY小海43 分钟前
【C++】AVL树实现
开发语言·数据结构·c++
workflower1 小时前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
Roc-xb1 小时前
ModuleNotFoundError: No module named ‘conda_token‘
开发语言·python·conda
怪兽20141 小时前
IntentService 的应用场景和使用方式?
android·面试
人工干智能1 小时前
Python 开发中:`.ipynb`(Jupyter Notebook 文件)和 `.py`(Python 脚本文件)
开发语言·python·jupyter
Jeled2 小时前
云信im在Android中的使用2
android
Jay-juice2 小时前
QT信号与槽
开发语言·qt
fs哆哆2 小时前
在VB.NET中,有没有 ?.这个运算符
java·开发语言·.net