[ 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
    }
}
相关推荐
Kapaseker3 小时前
Android 开放这扇门,正在被 Google 从里面反锁
android·kotlin
唐青枫4 小时前
别再把 Util 类堆成杂物间:Kotlin Extension 扩展函数与扩展属性实战详解
kotlin
plainGeekDev20 小时前
依赖管理 → Version Catalog
android·java·kotlin
黄林晴1 天前
KMP 正式支持 SwiftPM:CocoaPods 迁移配置指南
android·ios·kotlin
花燃柳卧2 天前
跨平台路由组件工程源码补充上传
android·flutter·kotlin
TechNomad2 天前
Kotlin类对象与接口详解
android·kotlin
Ws_2 天前
Kotlin Multiplatform day2
kotlin
TechNomad3 天前
Kotlin_Lambda编程详解
android·kotlin
plainGeekDev3 天前
ProGuard → R8
android·java·kotlin
aidou13143 天前
Kotlin中沉浸式状态栏显示布局
android·kotlin·windowmanager·沉浸式状态栏·insetslistener·insetscompat·systembars