ReadOnlyProperty - this is an interface that is predefined in the library to help with the delegation. This is for getting a value only.
ReadWriteProperty - this is an interface that is predefined in the library to help with the delegation. This is for getting and setting a value.
Note:
1. There is a note in the IDE that says about ReadWriteProperty that there is no need to extend as long as you have the same signature with the function declared in its interface. This consumes my time since you cannot access the function of an interface without implementing it.
2. When you override, you do not need to include the keyword operator.
===================
fun main(){
val neededHelpValue1: String by Helper1
println(neededHelpValue1)
var neededHelpValue2: String by Helper2
println(neededHelpValue2)
neededHelpValue2 = "let us change data 2"
println(neededHelpValue2)
}
object Helper1: ReadOnlyProperty<Any?, String> {
private const val h1: String = "Data 1"
override fun getValue(thisRef: Any?, property: KProperty<*>): String {
return h1
}
}
object Helper2: ReadWriteProperty<Any?, String> {
var h2: String = "Data 2"
override fun getValue(thisRef: Any?, property: KProperty<*>): String {
return h2
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
h2 = value
}
}
===================
Result:
Data 1
Data 2
let us change data 2
YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
