Kotlin中的参考Java接口静态字段 [英] Reference Java interface static fields in Kotlin

查看:124
本文介绍了Kotlin中的参考Java接口静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从Kotlin引用Java接口字段吗?我有这个Java接口:

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";
}

我在Kotlin中实现了

And I implement it in Kotlin:

object UserEntry : BaseColumns {
    // some code
}

当我尝试UserEntry._ID时,会得到未解决的参考.如何访问_ID?我想念什么吗?谢谢!

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

推荐答案

在Kotlin中,与Java不同,接口的静态成员不会派生,并且在不限定接口名称的情况下不能在子类中调用.

In Kotlin, unlike Java, static members of interfaces are not derived and cannot be called in subclasses without qualifying the interface name.

您应该通过BaseColumns引用_ID:BaseColumns._ID将起作用.

You should reference _ID through BaseColumns: BaseColumns._ID will work.

对于类而言,这似乎是不同的:基类的静态成员的非限定名称可以解决该问题,但该成员仍未继承.

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中的参考Java接口静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆