简体   繁体   中英

Kotlin / Java - Interface implementation + Inheritance

I have the following classes and interface:

class A() {
    fun one() {...}
    fun two() {...}
}

class B(): A, C {
    fun tree() {...}
}

interface C {
    fun one()
    fun two()
    fun tree()
}

As you can class B extends A and also implements interface C. The problem is that in Kotlin class B which is the actual implementor of C does not have the 2 first funcs and therefore not implementing the right functions for the interface. Is there a right way to do such thing?

  1. The A class needs to be marked open .
  2. The super class A of the class B needs to be initialized (ie A() )
  3. The B.tree() method requires override modifier

Apart of that, it should work...

open class A {
    fun one() {}
    fun two() {}
}

class B: A(), C {
    override fun tree() {}
}

interface C {
    fun one()
    fun two()
    fun tree()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM