如何在 Kotlin 中定义静态扩展方法?这可能吗?我目前有一个扩展方法,如下所示。
public fun Uber.doMagic(context: Context) {
// ...
}
可以在实例上调用上述扩展。
uberInstance.doMagic(context) // Instance method
但我如何使其成为静态方法,如下所示。
Uber.doMagic(context) // Static or class method
实现Uber.doMagic(context)
,您可以编写一个扩展伴生对象 https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects of Uber
(需要伴随对象声明):
class Uber {
companion object {}
}
fun Uber.Companion.doMagic(context: Context) { }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)