49.属性委托
作者:
写代码的向日葵 | 来源:发表于
2019-10-04 23:31 被阅读0次
1.代码写法如下:
fun main(args: Array<String>) {
val bigHeadSon = BigHeadSon()
bigHeadSon.money= 200
println(bigHeadSon.money)
}
class BigHeadSon {
var money: Int by Monther()
}
class Monther {
/**
* 儿子取压岁钱
*/
operator fun getValue(bigHeadSon: BigHeadSon, property: KProperty<*>): Int {
return sonMoney
}
/**
* 儿子存压岁钱 i:设置的值
*/
operator fun setValue(bigHeadSon: BigHeadSon, property: KProperty<*>, money: Int) {
sonMoney += 50
minMoney += money - 50
}
var sonMoney = 0
var minMoney = 0
}
- 就是把一个属性的get和set方法委托给一个对象来处理
本文标题:49.属性委托
本文链接:https://www.haomeiwen.com/subject/ubxepctx.html
网友评论