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

相关方法

相关推荐
福大大架构师每日一题1 天前
milvus v2.6.8 发布:搜索高亮上线,性能与稳定性全面跃升,生产环境强烈推荐升级
android·java·milvus
草莓熊Lotso1 天前
脉脉独家【AI创作者xAMA】| 开启智能创作新时代
android·java·开发语言·c++·人工智能·脉脉
李坤林1 天前
Android Binder详解【5】 ServiceManager
android·binder
Ya-Jun1 天前
Android 扫雷游戏项目设计报告
android·游戏
_李小白1 天前
【Android 性能分析】第五天:Perfetto UI分析CPU
android·ui
MindCareers1 天前
Beta Sprint Day 1-2: Alpha Issue Fixes Initiated + Mobile Project Setup
android·c语言·数据库·c++·qt·sprint·issue
龚礼鹏1 天前
Android应用程序 c/c++ 崩溃排查流程三——ndk-stack工具使用
android
zhengfei6111 天前
CVE-2025-13156 - Vitepos WooCommerce 的销售(POS) 系统漏洞
android
奥陌陌1 天前
自定义view, 图片右上角显示数字
android
TheNextByte11 天前
将照片从Mac传输到Android 7 种可行方法
android·macos·gitee