gpt4 book ai didi

kotlin - 有没有更干的方式来使用 Kotlin Delegate 属性 observable?

转载 作者:行者123 更新时间:2023-12-02 13:37:30 28 4
gpt4 key购买 nike

我正在使用可观察模式来跟踪对象的变化。为了实现这一点,我使用了 observable 中的构建。来自 Kotlin 。一切对我来说都很好,但是为了跟踪一些更改,我必须为每个属性重复相同的代码。这是我的代码:

class Employee(
id: String,
name: String,
surname: String,
age: Int,
salary: Int) {


val changes = HashMap<String, Pair<Any, Any>>()


val id = id //Id is immutable

var name: String by Delegates.observable(name) { prop, old, new ->
if (old != new) {
changes.put(prop.name, Pair(old, new))
println("${prop.name} has changed from $old to $new")
}
}

var surname: String by Delegates.observable(surname) { prop, old, new ->
if (old != new) {
changes.put(prop.name, Pair(old, new))
println("${prop.name} has changed from $old to $new")
}
}

var age: Int by Delegates.observable(age) { prop, old, new ->
if (old != new) {
changes.put(prop.name, Pair(old, new))
println("${prop.name} has changed from $old to $new")
}
}

var salary: Int by Delegates.observable(salary) { prop, old, new ->
if (old != new) {
changes.put(prop.name, Pair(old, new))
println("${prop.name} has changed from $old to $new")
}
}

}

如您所见,我正在为每个属性重复这些代码行:
by Delegates.observable(name) { prop, old, new ->
if (old != new) {
changes.put(prop.name, Pair(old, new))
println("${prop.name} has changed from $old to $new")
}
}

有没有人有想法让这段代码更干,这样我就不必到处复制和粘贴这些行了,我确实在网上看了但找不到在其他地方定义逻辑并将其应用于类(class)。

最佳答案

您不需要显式地重新声明这样的方法。你可以很容易地在 Kotlin 中传递方法引用。所以你可以这样做:

val age = Delegates.observable(age, ::handler) // Notice `::handler`
// repeat for the others...

// And this is the actual function:
fun handler(prop: KProperty<*>, old: Any, new: Any){
if (old != new) {
changes.put(prop.name, old to new)
println("${prop.name} has changed from $old to $new")
}
}
::handler将方法引用传递给处理它们的方法。您仍然需要重复初始化,但不需要多次创建相同的处理程序方法。

此外,如果 id是一个 val,你不用它做任何事情,你可以这样做:
class Employee(
val id: String, // Adding `val` or `var` in the constructor makes it an actual variable, instead of just local to the constructor. ***NOTE:*** This only applies to primary constructors.
name: String,
surname: String,
age: Int,
salary: Int) {

关于kotlin - 有没有更干的方式来使用 Kotlin Delegate 属性 observable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53205718/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com