Kotlin Delegates.notNull用法及代码示例

本文方法及代码示例基于Kotlin 2.1.20 Released

Delegates.notNull 所在包 kotlin.properties.Delegates.notNull,其相关用法介绍如下:

用法:

kotlin 复制代码
fun <T : Any> notNull(): ReadWriteProperty<Any?, T>

返回具有非 null 值的读/写属性的属性委托,该值不是在对象构造期间而是在以后初始化。在分配初始值之前尝试读取属性会导致异常。

示例代码:

kotlin 复制代码
import kotlin.properties.Delegates

import kotlin.test.*

fun main(args: Array<String>) {
    //sampleStart
    var max: Int by Delegates.notNull()

    // println(max) // will fail with IllegalStateException

    max = 10
    println(max) // 10
    //sampleEnd
}

// 输出
10

相关方法

相关推荐
Swizard1 分钟前
别让 AI 假装在工作:Android "Vibe Coding" 的生存指南
android·java·vibe coding
电饭叔30 分钟前
《python语言程序设计》2018版--第8章14题利用字符串输入作为一个信用卡号之一(Luhn算法解释)
android·java·python
QING6181 小时前
Jetpack Compose Brush API 详解 —— 新手指南
android·kotlin·android jetpack
鹿里噜哩1 小时前
Spring Authorization Server 打造认证中心(二)自定义数据库表
spring boot·后端·kotlin
Huanzhi_Lin1 小时前
安卓连接夜神模拟器命令及原理
android
AllBlue1 小时前
unity调用安卓方法
android·unity·游戏引擎
PWRJOY2 小时前
Android Studio中安卓模拟器打不开,报错The emulator process for AVD has terminated
android·ide·android studio
凛_Lin~~2 小时前
安卓 Java线程八股文 (线程、多线程、线程池、线程安全)
android·java·开发语言
城东米粉儿2 小时前
关于 PathMeasure 笔记
android
用户815099944112 小时前
浅谈RecyclerView缓存
android