PropertyModel 无法通过 get() 处理 Kotlin 的私有字段

PropertyModel cannot work with Kotlin's private field with get()

如果 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 }
}

有什么方法可以解决这个问题(保留 private 修饰符)?

(检票口 7.9.0,Kotlin 1.2)

由于模型需要读写,所以你的模型有必要属性 with backing field

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 作为模型,它在第二个代码片段中。