Kotlin->Kotlin委托


* 自定义属性委托
	+ `val/var <属性名>: <类型> by <表达式>`
	+ `thisRef`:委托者
	+ `this`:代理者
	+ `property`:修改的属性
	+ `value`:赋值



// 委托者
class Principal {
var intValue: Int by Delegate()
}
// 代理者
class Delegate {
var tempValue : Int = 0
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
println(“委托者 t h i s R e f 读取代理者 {thisRef} 读取 代理者 thisRef读取代理者{this} 属性名 ${property.name}”)
return tempValue
}

operator fun setValue(thisRef: Any?, property: KProperty<\*>, value: Int) {
    tempValue = value
    println("代理者${this} 修改 委托者${thisRef} 属性名 ${property.name} = $value")
}

}
// 使用方式
Principal().apply {
intValue = 26
}.apply {
println(this.intValue)
}
// log result
代理者Delegate@506c589e 修改 委托者Principal@69d0a921 属性名 intValue = 26
委托者Principal@69d0a921 读取 代理者Delegate@506c589e 属性名 intValue
26


* 官方属性委托
	+ `ReadOnlyProperty<in T, out V>`:只读接口,只能重载getValue方法
	+ `ReadWriteProperty<in T, V>`:读写接口,只能重载getValue和setValue方法



class MyReadOnlyProperty : ReadOnlyProperty<Any, Int> {
override operator fun getValue(thisRef: Any, property: KProperty<*>): Int {
println(“只读委托者 t h i s R e f 读取代理者 {thisRef} 读取 代理者 thisRef读取代理者{this} 属性名 ${property.name}”)
return 26
}
}

class MyReadWriteProperty : ReadWriteProperty<Any, Int> {
var tempValue : Int = 0
override operator fun getValue(thisRef: Any, property: KProperty<*>): Int {
println(“读写委托者 t h i s R e f 读取代理者 {thisRef} 读取 代理者 thisRef读取代理者{this} 属性名 ${property.name}”)
return tempValue
}

override operator fun setValue(thisRef: Any, property: KProperty<\*>, value: Int) {
    println("读写委托者${thisRef} 修改 代理者${this} 属性名 ${property.name} = $value")
    tempValue = value
}

}
class MyPrincipal{
val intValue1 by MyReadOnlyProperty()
var intValue2 by MyReadWriteProperty()
}
// 使用方式
println(MyPrincipal().intValue1)
MyPrincipal().apply {
intValue2 = 27
}.apply {
println(this.intValue2)
}
// log result
只读委托者MyPrincipal@5387f9e0 读取 代理者MyReadOnlyProperty@6e5e91e4 属性名 intValue1
26
读写委托者MyPrincipal@2cdf8d8a 修改 代理者MyReadWriteProperty@30946e09 属性名 intValue2 = 27
读写委托者MyPrincipal@2cdf8d8a 读取 代理者MyReadWriteProperty@30946e09 属性名 intValue2
27


* 延迟属性委托`lazy`
	+ 第一次调用会执行lambda表达式`{}`所有代码
	+ 后续调用只会执行lambda表达式`{}`最后一行代码



val intLazyValue : Int by lazy{
println(“first create”)
26
}
println(intLazyValue)
println(intLazyValue)
println(intLazyValue)
// log result
first create
26
26
26


* 可观察属性委托`observable`
	+ 观察一个属性的旧值和新值
	+ `observable`:返回一个实现了`ObservableProperty<T>(initialValue)`的单例
	+ `initialValue`:表示初始值
	+ `onChange`:属性修改时,先回调`ObservableProperty.setValue`方法,再回调`Delegates.observable.afterChange`方法,最终回调我们实现的`onChange`方法
		- `property`:被赋值的属性
		- `oldValue`:旧值
		- `newValue`:新值



public inline fun observable(initialValue: T,
crossinline onChange: (property: KProperty<*>, oldValue: T, newValue: T) -> Unit) : ReadWriteProperty<Any?, T> =
object : ObservableProperty(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(property, oldValue, newValue)
}
// 使用方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值