[ 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
    }
}
相关推荐
梓仁沐白15 小时前
【Kotlin】协程
开发语言·python·kotlin
WAsbry16 小时前
现代 Android 开发自定义主题实战指南
android·kotlin·material design
梓仁沐白18 小时前
【Kotlin】注解&反射&扩展
开发语言·python·kotlin
移动开发者1号1 天前
Jetpack Compose瀑布流实现方案
android·kotlin
移动开发者1号1 天前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·kotlin
移动开发者1号1 天前
ListView与RecyclerView区别总结
android·kotlin
移动开发者1号1 天前
OkHttp 3.0源码解析:从设计理念到核心实现
android·kotlin
casual_clover2 天前
Android 之 kotlin语言学习笔记三(Kotlin-Java 互操作)
android·java·kotlin
梓仁沐白2 天前
【Kotlin】数字&字符串&数组&集合
android·开发语言·kotlin
Dola_Pan2 天前
Android四大组件通讯指南:Kotlin版组件茶话会
android·开发语言·kotlin