2017-06-21 81 views
0

一个接口,这是我的KotlinInterface.kt如何返回像javawith科特林

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 
     } 
    } 
} 

我想知道如何r E打开与科特林喜欢用java,谢谢大家

回答

1

在科特林一个界面,您可以创建一个实现这样的接口的匿名类:

object : KotlinInterface { 
    override fun fun1() { 
     ... 
    } 
    override fun fun2() { 
     ... 
    } 
} 

这将会给你的匿名类的引用。你只需要返回。

+0

感谢您的帮助 –

0
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. 
     } 

    } 
} 
}