[ 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
    }
}
相关推荐
雨白5 小时前
深入理解 Kotlin 协程 (三):返璞归真,探寻协程基础设施的底层基石
kotlin
call me by ur name7 小时前
ERNIE 5.0 Technical Report论文解读
android·开发语言·人工智能·机器学习·ai·kotlin
catch_pig8 小时前
告别繁琐配置:Catchpig Detekt Plugin 让 Kotlin/Android 代码检查开箱即用
kotlin·android studio·代码规范
Fate_I_C12 小时前
Kotlin 内部类和嵌套类
java·开发语言·kotlin
千码君201614 小时前
kotlin:jetpack compose 生成动态可控的动画
vue.js·elementui·kotlin
alexhilton1 天前
Jetpack Compose元球边缘效果
android·kotlin·android jetpack
Kiri霧1 天前
Kotlin递归
android·开发语言·kotlin
普通网友1 天前
Android开发:使用Kotlin+协程+自定义注解+Retrofit的网络框架
android·kotlin·retrofit
常利兵1 天前
Kotlin抽象类与接口:相爱相杀的编程“CP”
android·开发语言·kotlin
Arkerman_Liwei1 天前
Android 新开发模式深度实践:Kotlin + 协程 + Flow+MVVM
android·开发语言·kotlin