[ 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
    }
}
相关推荐
Kapaseker1 天前
别再只用 Loading、Success、Error 表示所有页面状态了
android·kotlin
雨白2 天前
深入理解 Kotlin 协程 (九):互通有无,解构 Channel 缓冲策略与底层无锁机制
android·kotlin
蜡台3 天前
Android WebView 设计指南
android·java·kotlin
Kapaseker3 天前
深入理解 Compose 副作用
kotlin
雨白4 天前
深入理解 Kotlin 协程 (八):拾遗补阙,探秘官方框架的调度细节与取消闭环
android·kotlin
GitLqr4 天前
别被“Flutter 传感器延迟 150ms”带偏了:这可能只是你的实现方式错了
flutter·架构·kotlin
我命由我123454 天前
Android 在构建过程中,发现有两个依赖库都包含了相同路径的资源文件
android·java·开发语言·java-ee·kotlin·android-studio·android runtime
Coffeeee4 天前
谷歌的一个优化建议,让我重新学了一遍Android里面如何正确处理位图
android·google·kotlin
Android打工仔6 天前
Continuation 到底是谁创建的?
android·kotlin
Android-Flutter6 天前
android fragment 使用
android·kotlin