如何返回像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. } } } }
- Spring Security,Method Security注解(@Secured)不工作(java config)
- 第一个参数是带有一个方法的类时,不能用lambda替换SAM构造函数
- 使用Espresso的Android仪器测试失败:NoSuchMethodError javax.inject.Provider中的get()
- 用于Java的Beep库?
- 在Parent类中声明与Child类相同的属性
- 无法在IntelliJ中编译
- 如何从Kotlin的Java类型数组中获取javaclass?
- Mockitounit testing:在一个类的所有方法调用中,返回“true”
- Gradle + JUnit + Android数据绑定