如何在Kotlin子类上访问Java静态方法? [英] How do I access a Java static method on a Kotlin subclass?

查看:1113
本文介绍了如何在Kotlin子类上访问Java静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了Java类的Kotlin子类:

I've created a Kotlin subclass of a Java class:

class AlarmReceiver : WakefulBroadcastReceiver() {

    companion object {
        const val ACTION_NOTIFY = "..."
    }

    override fun onReceive(context: Context, intent: Intent) { ... }
}

WakefulBroadcastReceiver 有两种静态方法:

WakefulBroadcastReceiver has two static methods:

  • static boolean completeWakefulIntent(Intent intent)
  • static ComponentName startWakefulService(Context context, Intent intent)
  • static boolean completeWakefulIntent(Intent intent)
  • static ComponentName startWakefulService(Context context, Intent intent)

并从我的AlarmReceiver类中调用它们,就像我期望的那样.但是,我想在Kotlin子类之外调用这些方法之一 .

and calling these from within my AlarmReceiver class works just as I expect. However, I'd like to call one of these methods outside of my Kotlin subclass.

如果我尝试从另一个Kotlin类尝试AlarmReceiver.completeWakefulIntent(intent),则会收到以下编译错误:

If I try AlarmReceiver.completeWakefulIntent(intent) from a different Kotlin class, I get the following compilation error:

未解决的参考:completeWakefulIntent

Unresolved reference: completeWakefulIntent

我认为这是因为编译器正在尝试解析AlarmReceiver的伴随对象上的方法,而不是从其超类中查找继承的方法.解决方法是,我可以直接在AlarmReceiver.Companion上定义具有相同签名的方法:

I think this is because the compiler is trying to resolve the method on AlarmReceiver's companion object instead of finding the inherited method from its superclass. As a workaround, I can directly define a method with the same signature on AlarmReceiver.Companion:

class AlarmReceiver : WakefulBroadcastReceiver() {

    companion object {
        const val ACTION_NOTIFY = "..."

        // Just call the superclass implementation for now
        fun completeWakefulIntent(intent: Intent): Boolean =
            WakefulBroadcastReceiver.completeWakefulIntent(intent)
    }

    ...
}

我认为,如果我依赖于Java子类的默认继承方法,这种行为将具有与我相同的行为,但是我想知道是否有更好的方法可以做到这一点.

I think this has the same behavior I would have gotten had I relied on the default inherited method from a Java subclass, but I'm wondering if there's a better way to do this.

有没有办法在Kotlin子类上调用继承的静态Java方法?

Is there a way to call an inherited static Java method on a Kotlin subclass?

推荐答案

在Kotlin中,与Java不同,静态成员不会被子类继承,即使可以在没有基类名称的子类中调用静态成员.

In Kotlin, unlike Java, static members are not inherited by subclasses, even though they can be called inside a subclass without the base class name.

在子类之外,您必须使用基类名称来调用基类静态函数:

Outside a subclass, you have to call the base class static functions using the base class name:

WakefulBroadcastReceiver.completeWakefulIntent(intent)

此行为似乎在伴随对象概念之内:层次结构中的类的伴随对象没有相互包含.

This behavior seems to lie within the companion objects concept: companion objects of classes in a hierarchy are not included into each other.

有趣的是,对于接口,情况有些不同:与Java中的行为相同.

Interestingly, for interfaces the situation is a bit different: interface static members cannot be referenced in subclasses without the interface name. This behavior is the same to that in Java.

这篇关于如何在Kotlin子类上访问Java静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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