我想在我的 Kotlin 接口中为方法提供默认实现:
interface INavItem : Comparable<INavItem> {
val order: Int
@JvmDefault
override fun compareTo(other: INavItem): Int = order.compareTo(other.order)
}
但@JvmDefault
突出显示错误:
仅从 JVM 目标 1.8 起才支持“@JvmDefault”。使用 '-jvm-target 1.8 重新编译
JVM 目标 1.8 设置存在于根项目构建文件中:
project.ext {
javaVersion = JavaVersion.VERSION_1_8
// other properties
}
我在模块构建文件中使用它:
android {
compileOptions {
sourceCompatibility rootProject.ext.javaVersion
targetCompatibility rootProject.ext.javaVersion
}
// ...
}
注意:Java 源代码中提供了 Java 8 功能(lambdas 等)。
source/targetCompatibility
是否存在通过 Kotlin lint 可见的变量分配的任何方式?