【问题标题】:PropertyModel cannot work with Kotlin's private field with get()PropertyModel 不能与 Kotlin 的私有字段一起使用 get()
【发布时间】:2018-01-16 04:59:30
【问题描述】:

如果一个 kotlin 的模型有一个字段:

class MyModel {
  private val theValue: Double
    get()  { return 1.0 }
}

在检票口页面:

new PropertyModel(model , "theValue")

它会失败:

WicketRuntimeException: Property could not be resolved for class: class MyModel expression: theValue

解决办法:去掉私有修饰符:

class MyModel {
  val theValue: Double
    get()  { return 1.0 }
}

有没有办法解决这个问题(保留私有修饰符)?

(检票口 7.9.0 ,Kotlin 1.2)

【问题讨论】:

  • 如果有必要从课堂外访问它,为什么它应该是私有的?
  • 谢谢。我把它当作 java 的私有字段和公共 getter。

标签: kotlin wicket


【解决方案1】:

由于模型需要读写,所以你的模型必须有一个带有支持字段的属性。

class MyModel {
  private val theValue: Double
    get()  { return 1.0 }
}

没有支持字段,即使您删除了 private 修饰符。

试试这样:

class MyModel {
  var theValue = 1.0
}

或者如果您需要开箱即用的equals()hashCode() 等:

data class MyModel(var theValue: Double = 1.0)

注意:Wicket 是一个 Java 框架。在 documentation 中明确指出,您需要一个 Java bean 作为模型,它在第二个代码 sn-p 中。

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2021-12-08
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多