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()
}
相关推荐
sdgsdgdsgc1 小时前
Next.js企业级应用开发:SSR、ISR与性能监控方案
开发语言·前端·javascript
rit84324995 小时前
基于MATLAB的模糊图像复原
开发语言·matlab
yueqc15 小时前
Kotlin 协程 Flow 操作符总结
kotlin·协程·flow
fie88895 小时前
基于MATLAB的声呐图像特征提取与显示
开发语言·人工智能
_extraordinary_6 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
fanged7 小时前
天马G前端的使用
android·游戏
@。1247 小时前
对于灰度发布(金丝雀发布)的了解
开发语言·前端
hsjkdhs8 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
Full Stack Developme8 小时前
Python Redis 教程
开发语言·redis·python
qq_4924484469 小时前
Jmeter设置负载阶梯式压测场景(详解教程)
开发语言·python·jmeter