简体   繁体   English

在Kotlin中传递和使用函数作为构造函数参数

[英]Passing and using function as constructor argument in Kotlin

How to create a class that takes function as a constructor argument. 如何创建一个将函数作为构造函数参数的类。 Then, use this function at some later point in the class. 然后,在课程的稍后部分使用此功能。

You can have a property with a function type just like you would with any other type: 您可以拥有一个具有函数类型的属性,就像使用任何其他类型一样:

class A(val f: () -> Unit) {

    fun foo() {
        f()
    }

}

From here, you can pass that function to the constructor as a method reference: 从这里,您可以将该函数作为方法引用传递给构造函数:

fun bar() {
    println("this is bar")
}

val a = A(::bar)
a.foo()             // this is bar

Or as a lambda: 或者作为一个lambda:

val a = A({ println("this is the lambda") })

And you can even do the usual syntactic sugar for lambdas that are the last parameter of a function (although this is getting a little wild): 你甚至可以为lambda作为函数的最后一个参数做常用的语法糖(虽然这有点疯狂):

val a = A { println("this is the lambda") }

A real world example can be observed in SynchronizedLazyImpl , the class backing lazy delegates. SynchronizedLazyImpl可以观察到一个真实的例子,这是支持lazy委托的类。

public fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T> = 
    SynchronizedLazyImpl(initializer, lock)

When we use val x by lazy {...} , the initializer , passed as a lambda, is actually stored as a property in an instance of SynchronizedLazyImpl and called later when the corresponding val x is being accessed for the first time. 当我们val x by lazy {...}使用val x by lazy {...} ,作为lambda传递的initializer实际上作为属性存储在SynchronizedLazyImpl的实例中,并且在第一次访问相应的val x时稍后调用。

If you have more than one constructor declarations you can use this 如果您有多个构造函数声明,则可以使用它

...

private var listener : (() -> Unit)? = null

constructor(context: Context, listener: (() -> Unit)?) : this(context){
        this.listener = listener
}

constructor(context: Context) : super(context, attrs = null)

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin:将函数作为参数传递得到类型不匹配 - Kotlin: passing function as argument got Type mismatch Android-findViewById()-将参数传递给构造函数 - Android - findViewById() - passing argument to constructor 如何使用 kotlin 将 livedata 的结果作为 function 参数传递 - How to pass result of livedata as a function argument using kotlin 使用“ this”作为构造函数的参数 - Using “this” as an argument to a constructor 在kotlin中作为参数使用默认参数 - function as parameter in kotlin with default argument 在构造函数Kotlin中将泛型用作参数 - Using generic as parameter in constructor Kotlin Kotlin - 访问和使用构造函数参数 - Kotlin - accessing and using constructor parameters Android + Kotlin + Hilt:活动没有零参数构造函数 - Android + Kotlin + Hilt: Activity has no zero argument constructor 如何使用kotlin创建在构造函数中传递方法引用的泛型类的实例 - How to create instance of generic class passing a method reference in constructor with kotlin 使用导航参数传递数据时,如何解决错误ArrayList无法转换为Kotlin中的android.os.Parcelable []? - how to solve error ArrayList cannot be cast to android.os.Parcelable[] in Kotlin when passing data using navigation argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM