Kotlin默认实现从Java中实现

huangapple go评论80阅读模式
英文:

Kotlin default implementations from Java

问题

public class Employee implements PersonInterface {
}

我理解这样会为方法创建默认实现,以返回null、空列表或者通常设置的默认值。但是,如果我在 Java 中创建以下类(我预期它应该能够编译通过):

public class Employee implements PersonInterface {
}

我得到了错误:

类 Employee 必须要么声明为抽象或者实现 PersonInterface 中的抽象方法 getFirstName。

在Java中能否使用在Kotlin中定义的默认接口实现?


<details>
<summary>英文:</summary>

I have an interface in Kotlin that I want to have default implementations so that implementing classes will only have to implement a subset of the methods. Example:  

    interface PersonInterface {
        val firstname: String?
            get() = null
        val lastName: String?
            get() =  null
    
        val phoneNumbers: List&lt;String&gt;?
            get() = null
    
        val interests: List&lt;List&lt;String&gt;&gt;?
            get() = emptyList()
    
    }

This in my understanding would create default implementations for the methods to return `null` or an empty list or whatever I would have as defaults in general.  
But if I create the following class in **Java** (which I expected it would compile):  

    public class Employee implements PersonInterface {
    }

I get:  

&gt; Class Employee must either be declared abstract or implement abstract
&gt; method getFirstName in PersonInterface  

Is it possible to use default interface implementation defined in Kotlin from Java?

</details>


# 答案1
**得分**: 3

根据文档(https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html):

从JDK 1.8开始,Java中的接口可以包含默认方法。要使得 Kotlin 接口中的所有非抽象成员对于实现它们的 Java 类来说都是默认方法,需要使用 -Xjvm-default=all 编译选项编译 Kotlin 代码。

注意:在 Kotlin 1.4 之前,要生成默认方法,可以在这些方法上使用 @JvmDefault 注解。在 1.4 版本中,使用 -Xjvm-default=all 进行编译,通常情况下会像是给所有非抽象方法添加了 @JvmDefault 注解,并且使用 -Xjvm-default=enable 进行了编译。然而,在某些情况下它们的行为会有所不同。关于 Kotlin 1.4 中默认方法生成变化的详细信息,请参阅 Kotlin 博客上的这篇文章。

<details>
<summary>英文:</summary>

According to documentation (https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html):

Starting from JDK 1.8, interfaces in Java can contain default methods. To make all non-abstract members of Kotlin interfaces default for the Java classes implementing them, compile the Kotlin code with the -Xjvm-default=all compiler option

Note: Prior to Kotlin 1.4, to generate default methods, you could use the @JvmDefault annotation on these methods. Compiling with -Xjvm-default=all in 1.4 generally works as if you annotated all non-abstract methods of interfaces with @JvmDefaultand compiled with -Xjvm-default=enable. However, there are cases when their behavior differs. Detailed information about the changes in default methods generation in Kotlin 1.4 is provided in this post on the Kotlin blog.

</details>



huangapple
  • 本文由 发表于 2020年8月19日 16:48:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63483322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定