Kotlin:类继承了包含同伴对象的接口,但无权访问该接口的常量 [英] Kotlin: class inherits of an interface that contains companion object, but doesn't have access to the constants of the interface

查看:55
本文介绍了Kotlin:类继承了包含同伴对象的接口,但无权访问该接口的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是界面:

interface SomeInterface {
   companion object {
      const val MY_CONST = "the constant"
   }
}

然后,该类(没有主体,因为这只是一个示例):

And then, the class (without body because is only an example):

class SomeClass : SomeInterface

此后,当我尝试通过SomeClass调用常量时,它不允许我进行操作:

After this, when I try to call the constant through SomeClass, it not allows me:

SomeClass.MY_CONST

我该如何解决?

推荐答案

随播对象不是可继承的";它们在定义它们的上下文中完全合格(在您的情况下为 SomeInterface ).

Companion objects are not "inheritable"; they are fully qualified by the context in which they are defined (SomeInterface, in your case).

本质上,您需要编写:

SomeInterface.MY_CONST

这篇关于Kotlin:类继承了包含同伴对象的接口,但无权访问该接口的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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