PropertyModel无法与Kotlin';s带有get()的私有字段

PropertyModel无法与Kotlin';s带有get()的私有字段,kotlin,wicket,Kotlin,Wicket,如果kotlin的模型具有字段: class MyModel { private val theValue: Double get() { return 1.0 } } 在wicket页面中: new PropertyModel(model , "theValue") 它将失败: WicketRuntimeException: Property could not be resolved for class: class MyModel expression: theValue

如果kotlin的模型具有字段:

class MyModel {
  private val theValue: Double
    get()  { return 1.0 }
}
在wicket页面中:

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 }
}
有没有办法绕过这个问题(保留私有修改器)


(wicket 7.9.0,Kotlin 1.2)

由于需要读取和写入模型,因此您的模型必须具有带备份字段的属性

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框架。在这篇文章中,我们明确指出,您需要一个JavaBean作为模型,它在第二段代码中。

如果需要从类外访问它,为什么它应该是私有的?谢谢。我把它作为java的私有字段,并使用public getter。