[ 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
    }
}
相关推荐
Coffeeee7 小时前
面试被问到Compose的副作用不会,只怪我没好好学
android·kotlin·android jetpack
wuwu_q1 天前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
会跑的兔子2 天前
Android 16 Kotlin协程 第一部分
android·开发语言·kotlin
来来走走2 天前
Android开发(Kotlin) 高阶函数、内联函数
android·开发语言·kotlin
来来走走3 天前
Android开发(Kotlin) 扩展函数和运算符重载
android·开发语言·kotlin
wuwu_q3 天前
用通俗易懂 + Android 开发实战的方式,详细讲解 Kotlin Flow 中的 retryWhen 操作符
android·开发语言·kotlin
li-jia-wei3 天前
我在造一个编程语言,叫 Free
kotlin
Android-Flutter3 天前
kotlin - 显示HDR图(heic格式),使用GainMap算法,速度从5秒提升到0.6秒
android·kotlin
雨白3 天前
协程进阶:协作、互斥与共享状态管理
android·kotlin
studyForMokey4 天前
【Kotlin内联函数】
android·开发语言·kotlin