我开始和Kotlin玩arround,并通过custom getter阅读了一些关于可变val的内容.如here或Kotlin Coding Convention中所述,如果结果可以更改,则不应覆盖getter.
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
}
But just from the perspective of usage as in the guidelines where is the difference between the following two
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
}
从this个答案中,我可以看到getter override的值没有被存储.getter覆盖是否与赋值不同?也许是代表或是拉丁语?