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

相关方法

相关推荐
梦里花开知多少8 分钟前
浅谈ThreadPool
android·面试
帅次13 分钟前
单例初始化中的耗时操作如何拖死主线程
android·webview·android runtime
用户08748819991742 分钟前
Android 资源类型全解析及四大常用布局资源深度指南
android
海盐芝士不加糖1 小时前
我又又又辞职了,然后做了一款“离线版微信”
kotlin·app·android jetpack
火锅鸡的味道1 小时前
解决AOSP工程Android Studio打开卡顿
android·python·android studio
2501_915921431 小时前
2026 iOS 上架新趋势 iOS 发布流程模块化
android·ios·小程序·https·uni-app·iphone·webview
毕设源码-钟学长1 小时前
【开题答辩全过程】以 基于Android的高校二手交易系统为例,包含答辩的问题和答案
android
FLEMMINGS1 小时前
当 Android Studio 模拟器提示emulator failed to connect within 5 minutes
android·ide·android studio
鹏程十八少1 小时前
6. Android Shadow与众不同?一文解决插件化四大核心难题:ClassLoader冲突、Activity代理、资源隔离、动态更新(源码分析上)
android·前端·面试
不会写DN1 小时前
PHP mysqli 实用开发指南
android·开发语言·php