「Kotlin」オブザーバブル (Observable)を使うサンプル

2020年12月4日

説明
ハンドラはプロパティに割り当てるたびに呼び出されます。3つのパラメータがあり、割り当てられているプロパティ、古い値、そして新しい値です

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import kotlin.properties.Delegates
class Student {
var name: String by Delegates.observable("base value 123") {
prop, old, new ->
println("old value:$old -> new value:$new")
}
}
fun main(args: Array<String>) {
val st = Student()
st.name = "first value"
st.name = "second value"
}
import kotlin.properties.Delegates class Student { var name: String by Delegates.observable("base value 123") { prop, old, new -> println("old value:$old -> new value:$new") } } fun main(args: Array<String>) { val st = Student() st.name = "first value" st.name = "second value" }
import kotlin.properties.Delegates

class Student {
    var name: String by Delegates.observable("base value 123") {
        prop, old, new ->
        println("old value:$old -> new value:$new")
    }
}

fun main(args: Array<String>) {
    val st = Student()
    st.name = "first value"
    st.name = "second value"
}

実行結果:
old value:test123 -> new value:first value
old value:first value -> new value:second value

Kotlin

Posted by arkgame