在Kotlin中使用接口的默认功能实现 [英] Using default function implementation of interface in Kotlin

查看:2123
本文介绍了在Kotlin中使用接口的默认功能实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有默认实现的Kotlin界面,例如:

I have a Kotlin interface with a default implementation, for instance:

interface Foo {
    fun bar(): String {
        return "baz"
    }
}

在我尝试从Java实现此接口之前,这是可以的.当我这样做时,它说该类需要标记为抽象或实现方法bar().另外,当我尝试实现该方法时,无法调用super.bar().

This would be okay until I try to implement this interface from Java. When I do, it says the class need to be marked as abstract or implement the method bar(). Also when I try to implement the method, I am unable to call super.bar().

推荐答案

请参阅相关问题.

评论中有一条建议:

用Java(使用默认方法)编写您的界面,并且Java和Kotlin类均正确使用这些默认值

Write your interface in Java (with default methods) and both the Java and Kotlin classes correctly use those defaults

这篇关于在Kotlin中使用接口的默认功能实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆