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 我可以将具有反射值的对象变量设置为另一个对象的属性吗?_Kotlin - Fatal编程技术网

Kotlin 我可以将具有反射值的对象变量设置为另一个对象的属性吗?

Kotlin 我可以将具有反射值的对象变量设置为另一个对象的属性吗?,kotlin,Kotlin,我有一个类,它有一个类型为KMutableVar1的属性。该类的对象有一个变量指定给另一个类的属性的反射。我有一个函数,它应该接收第一个类的对象,并使用其类型为KMutableVar1的变量来确定要编辑第二个类的对象的哪个属性 天哪,这段话太糟糕了,我很抱歉,我不完全理解你为什么需要它,但我想它会有用的: import kotlin.reflect.KMutableProperty1 class Thing(var amount: Int, var id: Int) { fun edi

我有一个类,它有一个类型为KMutableVar1的属性。该类的对象有一个变量指定给另一个类的属性的反射。我有一个函数,它应该接收第一个类的对象,并使用其类型为KMutableVar1的变量来确定要编辑第二个类的对象的哪个属性


天哪,这段话太糟糕了,我很抱歉,我不完全理解你为什么需要它,但我想它会有用的:

import kotlin.reflect.KMutableProperty1

class Thing(var amount: Int, var id: Int) {
    fun editAttributes(editor: RemoteEdit) {
        val editing = editor.attributeToEdit
        editing.set(this, editor.newValue)
    }
}

class RemoteEdit(var attributeToEdit: KMutableProperty1<Thing, Int>, var newValue: Int)

fun main() {
    val bananas = Thing(amount = 12, id = 21)
    val remoteEditor = RemoteEdit(attributeToEdit = Thing::amount, newValue = 23)
    bananas.editAttributes(remoteEditor)
    println(bananas.amount) // prints 23
}
导入kotlin.reflect.KMutableProperty1
类事物(变量数量:Int,变量id:Int){
趣味编辑属性(编辑器:RemoteEdit){
val editing=editor.attributeToEdit
editing.set(这个,editor.newValue)
}
}
类RemoteEdit(变量attributeToEdit:KMutableProperty1,变量newValue:Int)
主要内容(){
val香蕉=东西(数量=12,id=21)
val remoteEditor=RemoteEdit(attributeToEdit=Thing::amount,newValue=23)
.editAttributes(remoteEditor)
println(bananas.amount)//打印23
}

我觉得这可能是XY问题,因为代码中有太多不寻常的东西。为什么要在正在编辑的类中实现通过反射更改属性值

我想,如果出于某种原因,您需要能够传递这些参数进行编辑,那么您需要一个类,但它可以实现自己使用它的函数:

class RemoteEdit<T, R>(var attributeToEdit: KMutableProperty1<T, R>, var newValue: R) {
    fun execute(item: T) {
        attributeToEdit.set(item, newValue)
    }
}

val bananas = Thing(amount = 12, id = 21)
val edit23 = RemoteEdit(Thing::amount, 23)
edit23.execute(bananas)
class RemoteEdit(变量attributeToEdit:KMutableProperty1,变量newValue:R){
乐趣执行(项目:T){
attributeToEdit.set(项,新值)
}
}
val香蕉=东西(数量=12,id=21)
val edit23=RemoteEdit(Thing::amount,23)
编辑23.执行(香蕉)
如果不需要传递这些信息,则只需要一个顶级函数:

fun <T, R> editProperty(item: T, attributeToEdit: KMutableProperty1<T, R>, newValue: R) =
    attributeToEdit.set(item, newValue)

val bananas = Thing(amount = 12, id = 21)
editProperty(bananas, Thing::amount, 23)
fun editProperty(项:T,属性编辑:KMutableProperty1,新值:R)=
attributeToEdit.set(项,新值)
val香蕉=东西(数量=12,id=21)
editProperty(香蕉,东西::数量,23)
fun <T, R> editProperty(item: T, attributeToEdit: KMutableProperty1<T, R>, newValue: R) =
    attributeToEdit.set(item, newValue)

val bananas = Thing(amount = 12, id = 21)
editProperty(bananas, Thing::amount, 23)