Kotlin委托Delegate托管by
import kotlin.reflect.KProperty
fun main() {
var user: String by MyDelegate()
user = "fly"
println(user)
}
class MyDelegate {
private var v: String? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return "property='${property.name}' getValue ${v}"
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
v = value
println("property='${property.name}' setValue -> $value")
}
}
property='user' setValue -> fly
property='user' getValue fly
import kotlin.properties.*
fun main() {
var len: Int by Delegates.observable(0) { prop, oldValue, newValue ->
println("$oldValue -> $newValue")
}
len = 2023
len = 2024
}
0 -> 2023
2023 -> 2024文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-701242.html
Delegates.observable追蹤觀察可變數(shù)據(jù)更新,Kotlin_zhangphil的博客-CSDN博客**Java觀察者模式的場(chǎng)景:一個(gè)女孩洗澡,被很多男孩偷看。女孩洞察后,搜索壞男孩,然后繼續(xù)洗澡。*//*男孩Boy.java*/import java.util.Observable;不定長(zhǎng)函參的Java觀察者模式更新數(shù)據(jù)傳遞import java.util.LinkedList;/** * 抽象被觀察者。Java觀察者模式 : Observer / Observable_zhangphil的博客-CSDN博客。不定長(zhǎng)函參的Java觀察者模式更新數(shù)據(jù)傳遞_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/132088085文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-701242.html
到了這里,關(guān)于Kotlin委托Delegate托管by的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!