简体   繁体   English

在Delegates.observable中使用propertyName参数

[英]Use of propertyName parameter in Delegates.observable

The syntax for Delegates.observable in Kotlin consist of "propertyName, oldValue , newValue". Kotlin中Delegates.observable的语法包括“ propertyName,oldValue和newValue”。

var name: String by Delegates.observable("no name") {
        d, old, new ->
        println("$old - $new")
    }

However, when I try to reuse the same observable for bunch of properties it won't work. 但是,当我尝试对一组属性重用相同的可观察值时,它将无法正常工作。

eg 例如

 private val observablePropertyDelegate 
= Delegates.observable("<none>")
     { pName, oldValue, newValue ->println("$pName is updated,$oldValue to $newValue")
            }
   var name: String by observablePropertyDelegate
   var name1: String by observablePropertyDelegate
   var name2: String  by observablePropertyDelegate

What confuses me is if we can not re-use the Observable delegates for different properties, then why does it contain the property name? 令我困惑的是,如果我们不能为不同的属性重用Observable委托,那么为什么它包含属性名呢? Is there any particular reason? 有什么特别的原因吗?

Why not following: 为什么不关注:

private val observablePropertyDelegate 
    = Delegates.observable("myOwnProperty","<none>")
         { oldValue, newValue ->println("myOwnProperty is updated,$oldValue to $newValue")
                }

Why do you say it does not work? 为什么说它不起作用?

This code: 这段代码:

import kotlin.properties.Delegates

class Test {
    private val observablePropertyDelegate
        = Delegates.observable("<none>")
    { pName, oldValue, newValue ->println("$pName is updated, $oldValue to $newValue")}

    var name: String by observablePropertyDelegate
    var name1: String by observablePropertyDelegate
    var name2: String  by observablePropertyDelegate
}

fun main(args: Array<String>) {
    val test = Test()

    test.name = "a"
    test.name1 = "b"
    test.name2 = "c"

    test.name = "d"
    test.name1 = "e"
    test.name2 = "f"
}

outputs this: 输出此:

property name (Kotlin reflection is not available) is updated, <none> to a
property name1 (Kotlin reflection is not available) is updated, a to b
property name2 (Kotlin reflection is not available) is updated, b to c
property name (Kotlin reflection is not available) is updated, c to d
property name1 (Kotlin reflection is not available) is updated, d to e
property name2 (Kotlin reflection is not available) is updated, e to f

which seems fine to me considering name , name1 and name2 are essentially aliases for the same field - this is the same situation as if you used the same backing field for mutliple properties. 考虑到namename1name2本质上是同一字段的别名,这对我来说似乎很好-这是相同的情况,就像您为mutiple属性使用相同的后备字段一样。

As for why property name is assed to the delegate - how else would you know which property was used to access the field? 至于为什么将属性名称分配给委托人-您还如何知道使用哪个属性来访问该字段? Also in your first example the delegate still prints correct message if you change the property name. 同样在您的第一个示例中,如果您更改属性名称,则委托仍会打印正确的消息。 However in the second one you need to remember to update delegate if change property name to make suer message stays correct. 但是在第二篇文章中,如果更改属性名称以使suer消息保持正确,则需要记住更新委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM