我开始和Kotlin一起玩arround,并用自定义getter读了一些关于mutable val的内容。如例如here或Kotlin Coding Convention所述,如果结果可以改变,则不应该重写吸气剂。Kotlin val差异getter覆盖vs作业
class SampleArray(val size: Int) {
val isEmpty get() = size == 0 // size is set at the beginning and does not change so this is ok
}
class SampleArray(var size: Int) {
fun isEmpty() { return size == 0 } // size is set at the beginning but can also change over time so function is prefered
}
但是,仅仅从使用的角度作为指导方针,其中从this答案,我可以看到,吸气剂覆盖的值不存储在以下两个
class SampleArray(val size: Int) {
val isEmpty get() = size == 0 // size can not change so this can be used instad of function
val isEmpty = size == 0 // isEmpty is assigned at the beginning ad will keep this value also if size could change
}
之间的差异。还有什么地方的getter覆盖是不同的分配?也许与代表或拉丁美洲?