How do I access a Java static method on a Kotlin subclass?

前端 未结 1 1419
南旧
南旧 2021-01-01 17:05

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

class AlarmReceiver : WakefulBroadcastReceiver() {

    companion object {
        const val ACTION_NOTIFY =         


        
相关标签:
1条回答
  • 2021-01-01 17:44

    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.

    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.

    0 讨论(0)
提交回复
热议问题