如何返回像javawith Kotlin接口
这是我的KotlinInterface.kt
interface KotlinInterface { fun fun1() fun fun2() }
这是我的JavaClass.java
public class JavaClass { public KotlinInterface test() { return new KotlinInterface() { @Override public void fun2() { } @Override public void fun1() { } }; }
}
这是我的KotlinClass.kt
class KotlinClass { fun test():KotlinInterface{ return KotlinInterface{ //how to return like java } } }
我想知道如何使用java返回与kotlin的接口,谢谢大家
在Kotlin中,您可以创建一个匿名类来实现如下的接口:
object : KotlinInterface { override fun fun1() { ... } override fun fun2() { ... } }
这将给你的匿名类提供参考。 你只需要返回。
class KotlinClass { fun test():KotlinInterface{ return object :KotlinInterface{ override fun fun1() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun fun2() { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } } } }