【发布时间】:2020-12-08 12:09:10
【问题描述】:
我在 Kotlin 中有一个接口,我希望它具有默认实现,以便实现类只需实现方法的子集。示例:
interface PersonInterface {
val firstname: String?
get() = null
val lastName: String?
get() = null
val phoneNumbers: List<String>?
get() = null
val interests: List<List<String>>?
get() = emptyList()
}
据我了解,这将为方法创建默认实现以返回 null 或一个空列表或我通常拥有的任何默认值。
但是,如果我在 Java 中创建以下类(我希望它会编译):
public class Employee implements PersonInterface {
}
我明白了:
Class Employee 必须声明为抽象或实现抽象 PersonInterface 中的方法 getFirstName
是否可以在 Java 中使用 Kotlin 中定义的默认接口实现?
【问题讨论】: