Can I reference Java interface fields from Kotlin? I have this Java interface:

public interface BaseColumns {
    public static final String _ID = "_id";
    public static final String _COUNT = "_count";
}

And I implement it in Kotlin:

object UserEntry : BaseColumns {
    // some code
}

I get Unresolved reference when I try UserEntry._ID. How can I access the _ID? Am I missing something? Thanks!

推荐答案

在Kotlin中,与Java不同,接口的静态成员不是派生的,如果不限定接口名,就不能在子类中调用.

你应该参考_IDBaseColumns:BaseColumns._ID行.

This seems to be different for classes: non-qualified name of a base class static member resolves to it, but the member is still not inherited.

Kotlin相关问答推荐

如何将消费者放入 Kotlin 的 map 中?

MyType.()在 Kotlin 中是什么意思?

Android Jetpack Compose:在空的 Compose 活动中找不到 OutlinedTextField (Material3)

如何使用函数类型或 lambdas 作为 Kotlin 上下文接收器的类型?

如何使用子变量在 Kotlin 中初始化父级

is return inside function definition 也是 kotlin 中的表达式

你怎么知道什么时候需要 yield()?

Kotlin 有垃圾收集器吗?如果是这样,它基于哪种算法?

内联函数导致单元测试代码覆盖率报告出错

使用最新的 com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2 库时未找到 RxTextView 和其他小部件

在 Spring Framework 5.1 中注册具有相同名称的测试 bean

Kotlin 单元测试 - 如何模拟 Companion 对象的组件?

如何将 Kotlin 日期中的字符串或时间戳格式化为指定的首选格式?

如何序列化/反序列化Kotlin密封类?

Kotlin flatMap - map

Kotlin var lazy init

将 androidx.constraintlayout:constraintlayout lib 更新到 2.0.2 版本后出现崩溃 isRtl () null 引用

Kotlin 的数据类 == C# 的 struct ?

函数引用和lambdas

java.lang.NoClassDefFoundError:解析失败:Lkotlin/time/MonoClock