gre*_*fox 6 spring properties kotlin kotlin-allopen
Kotlin的财产和开放财产有什么区别?下面的代码抱怨我将setter 设为私有,而Intellij则说开放属性不允许私有 setter 。什么是开放财产?
@RestController
open class ParameterController {
@Autowired
lateinit var parameterRepository: ParameterRepository
private set //error
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码无效,但此代码有效?
open class ItemPrice{
lateinit var type: String
private set // ok
}
Run Code Online (Sandbox Code Playgroud)
编辑:我正在使用spring-allopen插件,并且将类显式标记为open没什么区别。
What is an open property?
A open property that means its getter/setter(?) is not final. On the other hand, its getter & setter can be override by its subclasses.
In kotlin, everything is declared with final keyword except interface, annotation class, sealed class, enum class, variables, mutable property backing field and parameters, but the immutable variables & parameters are effectivily-final.
Due to the allopen
plugin will makes all of the properties & methods opened in the spring components.
However, a open property can't to makes a private setter, if the property is opened, for example:
//v--- allopen plugin will remove all `final` keyword, it is equivalent to `open`
open var value: String=""; private set
// ^--- ERROR:private set are not allowed
Run Code Online (Sandbox Code Playgroud)
So you must make the property as final explicitly, for example:
//v--- makes it final explicitly by `final` keyword
final var value: String =""; private set
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
885 次 |
最近记录: |