Dart 强迫我在子类中实现非抽象方法

swa*_*ale 3 dart

我正在尝试抽象类,我发现一个问题,我必须实现在子类中具有主体的非抽象方法

代码:

abstract class Animal{
void breathe(); //abstract method

void makeNoise(){
//non abstract method
print('making animal noises!');
}
}

abstract class IsFunny{
void makePeopleLaugh();//abstract method
}

class TVShow implements IsFunny{
String name;

@override
void makePeopleLaugh() {
// TODO: implement makePeopleLaugh
print("TV show is funny and make people laugh");
}
}

class Comedian extends Person implements IsFunny{
Comedian(String name, String nation) : super(name, nation);

@override
void makePeopleLaugh() {
// TODO: implement makePeopleLaugh
print('make people laugh');
}
}

class Person implements Animal{
String name,nation;

Person(this.name,this.nation);

//we must implement all the methods present in Abstract class and child should override the abstract methods
@override
void breathe() {
// TODO: implement breathe
print('person breathing through nostrils!');
}

//there should be no compulsion to override non abstract method
@override
void makeNoise() {
// TODO: implement makeNoise
print('shouting!');
}

}

void main(List arguments) {
var swapnil=new Person('swapnil','India');
swapnil.makeNoise();
swapnil.breathe();
print('${swapnil.name},${swapnil.nation}');
}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图不在我的 Person 类中实现 makeNoise 方法,但它给出错误并表示必须实现抽象方法。

这是错误还是我的概念错误

Abi*_*n47 10

您正在使用implements,它用于接口,而不是用于继承。您正在寻找的关键字是extends

abstract class Foo {
  void doThing() {
    print("I did a thing");
  }

  void doAnotherThing();
}

class Bar extends Foo {
  @override
  void doAnotherThing() {
    print("I did another thing");
  }
}
Run Code Online (Sandbox Code Playgroud)