package fenglei.demo.classExample
import kotlin.reflect.KProperty
/*
属性代理
*/
class Delegates {
val hello by lazy {
}
val hello2 by MyLazy()
var hello3 by MyLazy()
}
class MyLazy {
private var value: String? = null
// 代理的对象 代理属性
operator fun getValue(de: Delegates, property: KProperty<*>): String {
println("getValue:$de -> ${property.name}")
return value ?: ""
}
operator fun setValue(de: Delegates, property: KProperty<*>, value: String) {
println("getValue:$de -> ${property.name} = $value")
this.value = value
}
}
fun main(args: Array<String>) {
val de=Delegates()
de.hello2
de.hello3="3"
}