简体   繁体   English

Kotlin / Java - 接口实现 + 继承

[英]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.正如您可以类 B 扩展 A 并实现接口 C 一样。问题在于,在 Kotlin 中,作为 C 的实际实现者的类 B 没有前两个函数,因此没有为接口实现正确的功能。 Is there a right way to do such thing?有没有正确的方法来做这样的事情?

  1. The A class needs to be marked open . A类需要标记为open
  2. The super class A of the class B needs to be initialized (ie A() ) B类的超类A需要初始化(即A()
  3. The B.tree() method requires override modifier B.tree()方法需要override修饰符

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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