Description
Read about properties in Kotlin.
Add a custom setter to PropertyExample.propertyWithCounter so that the
counterproperty is incremented every timepropertyWithCounteris assigned to.
Kotlinの プロパティ を読んでください。
propertyWithCounterが割り当てられるたびにcounterプロパティがインクリメントされるように、カスタムセッターを PropertyExample.propertyWithCounter に追加してください。
Code
class PropertyExample() {
var counter = 0
var propertyWithCounter: Int? = null
set(v: Int?) {
field = v
counter++
}
}
Memo
- Kotlinのプロパティでは、アクセサ(
get/set)が暗黙的に生成される。Javaのように自前で実装する必要がないget()/set(value)には、カスタムゲッター/セッターを定義することができる