Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何为KMutableProperty参数赋值?_Kotlin - Fatal编程技术网

Kotlin 如何为KMutableProperty参数赋值?

Kotlin 如何为KMutableProperty参数赋值?,kotlin,Kotlin,在方法中,我希望接收KMutableProperty作为参数并为其赋值 另一个问题是,将参数传递给这种方法的正确方法是什么 基本上我想要这样的东西: class MyBinder { ... fun bind(property: KMutableProperty<Int>): Unit { property.set(internalIntValue) } } Kotlin 1.0不允许使用this::intProperty语法,但该语法目前正在使用中,并将很

在方法中,我希望接收KMutableProperty作为参数并为其赋值

另一个问题是,将参数传递给这种方法的正确方法是什么

基本上我想要这样的东西:

class MyBinder {
  ...

  fun bind(property: KMutableProperty<Int>): Unit {
     property.set(internalIntValue)
  }
}

Kotlin 1.0不允许使用
this::intProperty
语法,但该语法目前正在使用中,并将很快作为1.1(,)早期访问预览的一部分提供

考虑到这一点,我会考虑用另一种方式来描述你所描述的内容,例如制作<代码> BION/COD> >接受设置属性的lambda:

class MyBinder {
    fun bind(setProperty: (Int) -> Unit) {
        setProperty(internalIntValue)
    }
}

...

myBinder.bind { intProperty = it }
无论如何,要回答您关于设置
KMutableProperty
的值的问题:要设置某个属性的值,或者从技术上讲,要调用属性setter,您应该知道它的arity,或者属性(及其getter/setter)接受的参数数。文件中声明的属性不接受任何参数,成员属性和扩展属性需要一个参数(接收方实例),而作为扩展的成员属性需要两个参数。这类属性分别由
KMutableProperty
的以下子类型表示:
KMutableProperty0
KMutableProperty1
KMutableProperty2
——数字表示arity,其泛型参数表示接收器的类型。每种属性类型都有一个带有相应参数的
set
方法。一些例子:

fun setValue(property: KMutableProperty0<Int>, value: Int) {
    property.set(value)
}

fun setValue(property: KMutableProperty1<SomeType, Int>, instance: SomeType, value: Int) {
    property.set(instance, value)
}
fun setValue(属性:KMutableProperty0,值:Int){
属性集(值)
}
fun setValue(属性:KMutableProperty1,实例:SomeType,值:Int){
属性集(实例、值)
}

请注意,抽象
KMutableProperty
接口中没有
set
(或
get
)方法,因为不知道所需接收器参数的数量就无法声明它。

除Alexander的答案外,您还可以尝试以下方法:

import kotlin.reflect.KMutableProperty

class Binder {
  val internalIntValue = 10

  fun bind(self: Any, aProperty: KMutableProperty<Int>) {
    aProperty.setter.call(self, internalIntValue)
  }
}

class Foo {
  var bar = 1

  fun changeBar() {
    Binder().bind(this, Foo::bar)
  }
}

fun main(args: Array<String>) {
  val foo = Foo()

  assert(1 == foo.bar)

  foo.changeBar()

  assert(10 == foo.bar)
}
import kotlin.reflect.KMutableProperty
活页夹{
val internalIntValue=10
趣味绑定(self:Any,aProperty:KMutableProperty){
aProperty.setter.call(self,internalIntValue)
}
}
福班{
var bar=1
有趣的变更条(){
Binder().bind(这个,Foo::bar)
}
}
趣味主线(args:Array){
val foo=foo()
断言(1==foo.bar)
foo.changeBar()
断言(10==foo.bar)
}
做同样事情的更可靠/安全的方法:

fun <T> bind(self: T, aProperty: KMutableProperty1<T, Int>) {
  aProperty.set(self, internalIntValue)
}
fun绑定(self:T,aProperty:KMutableProperty1){
属性集(self,internalIntValue)
}
我感谢亚历山大。他的回答给了我先前的想法

fun <T> bind(self: T, aProperty: KMutableProperty1<T, Int>) {
  aProperty.set(self, internalIntValue)
}