【问题标题】:Kotlin - Is there a way to pass property values to another object with properties of the same name?Kotlin - 有没有办法将属性值传递给另一个具有同名属性的对象?
【发布时间】:2020-02-27 19:04:47
【问题描述】:

有没有办法将属性值传递给具有相同名称属性的另一个对象以避免“重复”代码?

例如,避免 CepReceiptsInfovalue 的类不同,但它们共享一些属性名称和类型:

   val cepReceiptsInfo = CepRecepitsInfo()
    cepReceiptsInfo.operationTimestamp = value.operationTimestamp
    cepReceiptsInfo.sentDate = value.sentDate
    cepReceiptsInfo.sentTime = value.sentTime
    cepReceiptsInfo.concept = value.concept
    cepReceiptsInfo.referenceNumber = value.referenceNumber
    cepReceiptsInfo.amount = value.amount
    cepReceiptsInfo.trackingKey = value.trackingKey
    cepReceiptsInfo.bankTarget = value.bankTarget
    cepReceiptsInfo.bankSource = value.bankSource
    cepReceiptsInfo.sourceClienteName = value.sourceClienteName
    cepReceiptsInfo.beneficiaryName = value.beneficiaryName
    cepReceiptsInfo.accountNumberTarget = value.accountNumberTarget
    cepReceiptsInfo.term = value.term
    cepReceiptsInfo.authorizationNumber = value.authorizationNumber
    cepReceiptsInfo.linkCep = value.linkCep
    cepReceiptsInfo.status = value.status
    cepReceiptsInfo.bankSourceRefund = value.bankSourceRefund
    cepReceiptsInfo.causeRefund = value.causeRefund
    cepReceiptsInfo.accountTargetRefund = value.accountTargetRefund
    cepReceiptsInfo.currency = value.currency
    cepReceiptsInfo.accountNumberSource = value.accountNumberSource
    cepReceiptsInfo.accountTypeSource = value.accountTypeSource
    cepReceiptsInfo.accountTypeTarget = value.accountTypeTarget
    cepReceiptsInfo.indicatorRefund = value.indicatorRefund
    cepReceiptsInfo.amountIntRefund = value.amountIntRefund
    cepReceiptsInfo.operationRefundTimestamp = value.operationRefundTimestamp
    cepReceiptsInfo.dateMovement = value.dateMovement
    cepReceiptsInfo.timeMovement = value.timeMovement
    cepReceiptsInfo.dateRefund = value.dateRefund
    cepReceiptsInfo.timeRefund = value.timeRefund

到 f.e. 之类的东西:

val cepReceiptsInfo = CepReceintsInfo()
cepReceiptsInfo.assignFrom(value)

两个类都是数据类

【问题讨论】:

    标签: kotlin data-class


    【解决方案1】:

    我不知道没有反射的方法。

    fun Any.assignFrom(other: Any) {
        val thisProperties = this::class.memberProperties
            .filterIsInstance<KMutableProperty<*>>()
            .map { it.name to it }
            .toMap()
        for (property in other::class.memberProperties){
            thisProperties[property.name]?.setter?.call(this, property.getter.call(other))
        }
    }
    

    【讨论】:

      【解决方案2】:

      正如 Tenfour04 所说,该语言不提供执行此操作的直接方法,因此您只能使用额外的编译时工具或运行时反射。

      由于这是一个相当普遍的问题,有一个名为 ModelMapper 的 Java 库可以为您解决此问题;与几乎所有 Java 库一样,您也可以在 Kotlin 中使用它。

      (我偶尔使用过它。它可以避免大量样板文件。虽然我发现它不像首页声称的那样重构安全……它还以 IDE 赢得的方式“隐藏”对字段的引用'不追踪。所以这不是一个完美的解决方案。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-20
        • 1970-01-01
        • 2017-06-20
        • 2019-07-30
        • 1970-01-01
        • 2019-11-27
        相关资源
        最近更新 更多