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

查看:47
本文介绍了在 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天全站免登陆