📅  最后修改于: 2020-12-30 07:05:27             🧑  作者: Mango
Kotlin通过引入新的关键字“ by”来支持“委托”设计模式。使用此关键字或委托方法,Kotlin允许派生类通过特定对象访问接口的所有已实现公共方法。以下示例演示了如何在Kotlin中发生这种情况。
interface Base {
fun printMe() //abstract method
}
class BaseImpl(val x: Int) : Base {
override fun printMe() { println(x) } //implementation of the method
}
class Derived(b: Base) : Base by b // delegating the public method on the object b
fun main(args: Array) {
val b = BaseImpl(10)
Derived(b).printMe() // prints 10 :: accessing the printMe() method
}
在示例中,我们有一个接口“ Base”及其抽象方法“ printme()”。在BaseImpl类中,我们正在实现此“ printme()”,后来在另一个类中,我们通过“ by”关键字使用了此实现。
上面的代码将在浏览器中产生以下输出。
10
在上一节中,我们了解了使用“ by”关键字的委托设计模式。在本节中,我们将学习使用Kotlin库中提到的一些标准方法来了解属性的委派。
委派意味着将责任转移到另一个类或方法。当某些地方已经声明了属性时,我们应该重用相同的代码对其进行初始化。在以下示例中,在示例中实现委托时,我们将使用Kotlin提供的一些标准委托方法和一些标准库函数。
Lazy是一个lambda函数,它接受属性作为输入,然后返回Lazy
val myVar: String by lazy {
"Hello"
}
fun main(args: Array) {
println(myVar +" My dear friend")
}
在上面的代码中,我们将变量“ myVar”传递给Lazy函数,该函数将值分配给其对象,并将其返回给main函数。以下是浏览器中的输出。
Hello My dear friend
Observable()使用两个参数来初始化对象,并将其返回给被调用的函数。在下面的示例中,我们将看到如何使用Observable()方法来实现委托。
import kotlin.properties.Delegates
class User {
var name: String by Delegates.observable("Welcome to Tutorialspoint.com") {
prop, old, new ->
println("$old -> $new")
}
}
fun main(args: Array) {
val user = User()
user.name = "first"
user.name = "second"
}
上面的代码将在浏览器中产生以下输出。
first -> second
通常,语法是委派“ by”关键字后的表达式。变量p的get()和set()方法将委派给Delegate类中定义的getValue()和setValue()方法。
class Example {
var p: String by Delegate()
}
对于上面的代码,以下是我们需要生成的委托类,以便在变量p中分配值。
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty): String {
return "$thisRef, thank you for delegating '${property.name}' to me!"
}
operator fun setValue(thisRef: Any?, property: KProperty, value: String) {
println("$value has been assigned to '${property.name} in $thisRef.'")
}
}
读取时,将调用getValue()方法,而在设置变量时将调用setValue()方法。