[ Kotlin ] Property Delegate

About Property Delegate

property delegate enable you get or set a property value

by DelegateObject.getValue or DelegateObject.setValue

in this way, Delegate object can be reused in similar places

Sources
kotlin 复制代码
fun main() {
    val hello = Hello()
    hello.name = "tom"
    println(hello.name)
}

class Hello {

    var name: String by Delegate()
}

class Delegate {

    private var value = ""

    operator fun getValue(thisRef: Any, property: KProperty<*>): String {
        return "${thisRef.javaClass.simpleName}::${property.name}=${value}"
    }

    operator fun setValue(thisRef: Any, property: KProperty<*>, value: String) {
        this.value = value
    }
}
相关推荐
heeheeai13 分钟前
决策树,随机森林,boost森林算法
算法·决策树·随机森林·kotlin·boost
用户095 小时前
停止滥用 Dispatchers.IO:Kotlin 协程调度器的深度陷阱与优化实战
android·面试·kotlin
明飞19876 小时前
isSuperclassOf 与is 与 ==的区别
kotlin
风起云涌~1 天前
【Android】kotlin.flow简介
android·开发语言·kotlin
魏思凡1 天前
爆肝一万多字,我准备了寿司 kotlin 协程原理
kotlin·源码阅读
studyForMokey1 天前
【Kotlin进阶】泛型的高级特性
android·开发语言·kotlin
袁震1 天前
Android-kotlin MVVM框架搭建+Retrofit二次封装
android·kotlin·mvvm·retrofit
jzlhll1231 天前
deepseek Kotlin Channel 详细学习指南
kotlin·channel
小趴菜82272 天前
安卓接入Kwai广告源
android·kotlin
程序员江同学2 天前
Kotlin 技术月报 | 2025 年 9 月
android·kotlin