私はKotlinでarroundを演奏し、カスタム・ゲッターで可変のvalを読み始めました。たとえばhereまたはKotlin Coding Conventionのように、結果が変わる可能性がある場合はゲッターをオーバーライドしないでください。コットン・ヴァル・ゲッター・オーバライド対代入
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
}
がある私はゲッターの値をオーバーライドすることを見ることができたガイドラインのような使用法の観点からは保存されません。ゲッターのオーバーライドが割り当てと異なるところに何か他のものがありますか?たぶん、代議員やlatinitと?