[ 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
    }
}
相关推荐
high一马平川16 小时前
# SystemUI Dagger2 精读 · 第 2 章 @Inject:1200 处构造注入,和 1 处例外
android·java·kotlin
小铁-Android18 小时前
CompletableDeferred:Kotlin 协程中用于一次性挂起通信的工具
android·kotlin
撩得Android一次心动1 天前
Jetpack Compose 知识点整理2【个人用】
android·笔记·学习·kotlin·compose
Ehtan_Zheng1 天前
Kotlin 伴生块:Android 项目怎么用,迁移时要注意什么
kotlin
JMchen1 天前
【Jetpack 内核实战 02】ViewModel:生命周期管理与数据持久化核心实战
kotlin·android studio·android jetpack
撩得Android一次心动2 天前
Jetpack Compose 知识点整理3【个人用】
android·笔记·学习·kotlin·android compose
Kapaseker2 天前
别再只会写 <T>:Kotlin 泛型的实战用法
android·kotlin
alexhilton2 天前
Room Fts4:全文搜索替代 SQL LIKE
android·kotlin·android jetpack