Junior — Middle
Milliseid argumente ja parameetreid võtab vastu delegaadi objekti getValue meetod?
sobes.tech AI
Vastus AI-lt
The getValue method of a delegate object in Kotlin accepts three parameters:
- thisRef — the object to which the delegate is applied (usually an instance of the class where the property is declared). The parameter type matches the type of the property owner.
- property — an object of type
KProperty<*>, which contains information about the property itself (e.g., the property name).
The method signature looks like this:
operator fun getValue(thisRef: Any?, property: KProperty<*>): T
where T is the type of the value returned by the property.
Example:
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
return "Value for property '${property.name}'"
}
}
class Example {
val prop: String by Delegate()
}
fun main() {
val e = Example()
println(e.prop) // Prints: Value for property 'prop'
}