Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Interface 如何实例化在Kotlin中实现接口的匿名类_Interface_Kotlin - Fatal编程技术网

Interface 如何实例化在Kotlin中实现接口的匿名类

Interface 如何实例化在Kotlin中实现接口的匿名类,interface,kotlin,Interface,Kotlin,在Java中,实例化接口对象就像newinterface()一样简单。。。并在AnimationListener private void doingSomething(Context context) { Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); animation.setAnimationListener(new Animation.Animatio

在Java中,实例化接口对象就像
newinterface()
一样简单。。。并在
AnimationListener

private void doingSomething(Context context) {
    Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
    animation.setAnimationListener(new Animation.AnimationListener() {
        // All the other override functions
    });
}
但是,在Kotlin中,当我们键入

private fun doingSomething(context: Context) {
    val animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in)
    animation.setAnimationListener(Animation.AnimationListener(){
        // All the other override functions
    })
}
It错误投诉未解决引用AnimationListener。

如中所述:

显然,最新的实现方法(使用Kotlin 1.0.5)现在没有括号,因为接口没有空构造函数

animation.setAnimationListener(object : Animation.AnimationListener {
    // All the other override functions
})

谢谢伟大的当我搜索时,它甚至没有触及任何东西。文件相当模糊。在网上搜索也找不到。找到这一点的唯一方法是手动编写Java代码,并将其转换。。。希望我的stackoverflow问题能帮助其他人快速找到它。谢谢我建议改进kotlin slack中的文档。我同意这些信息在文档中不是那么容易找到。听起来很棒!文档已经改进:并且@Ultimo\u对此表示抱歉。带括号的语法正确,可以创建一个匿名对象来扩展类并调用其默认构造函数。当实现一个接口时,括号实际上不应该在那里
animation.setAnimationListener(object : Animation.AnimationListener {
    // All the other override functions
})