简体   繁体   English

在Java中使用静态方法作为Kotlin中的扩展方法

[英]Use Static Method in Java as Extension Method in Kotlin

I have a static method declared in Java: 我有一个在Java中声明的静态方法:

class X {
    public static void foo(Y y) { … }
}

I would love to use this method as extension method for instances of type Y in Kotlin: 我想在Kotlin中使用此方法作为Y类型实例的扩展方法:

import X.foo
…
y.foo()

Is that possible? 那可能吗? I have control over all source code in question, eg to add annotations. 我可以控制所有相关的源代码,例如添加注释。

我不知道自动引用这些的方法,但编写自己的扩展只能包装现有的方法应该是可能的...

fun Y.foo() = X.foo(this)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM