当在一个类中继承或实现了一个接口时,如果该接口中有抽象方法没有被重写或继承,就会出现“Must override or implement a supertype”(必须重写或实现超类型)的错误提示。
解决这个问题的方法是在子类中重写或继承抽象方法, 或者在子类前加上 abstract 关键字,将子类也定义为抽象类。
示例代码:
interface MyInterface { fun doSomething() }
class MyClass : MyInterface { // 在此处没有重写 MyInterface 接口中的抽象方法,IDE 就会提示“Must override or implement a supertype” fun doOtherThing() { println("this is doOtherThing method") } }
// 解决方法一:在子类中重写抽象方法 class MySubClass: MyInterface { override fun doSomething() { println("this is doSomething method") } }
// 解决方法二:将子类也定义为抽象类 abstract class MyAbstractClass : MyInterface { // MyAbstractClass 依然没有重写 MyInterface 接口中的抽象方法,但不会出现“Must override or implement a supertype”错误提示 fun doOtherThing() { println("this is doOtherThing method") } }