Dart迫使我在子类中实现非抽象方法:已解决

问题描述 投票:0回答:3

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

代码:

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}');
}

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

是此错误还是我弄错了概念

dart abstract-class
3个回答
0
投票

我不认为这是一个错误。该方法仍在您正在[[implementing的抽象类中。我认为,您打算改为extend该类,在这种情况下,您将在覆盖中调用super.makeNoise()


0
投票
您正在使用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"); } }


0
投票
我在一段时间后找出解决方案,我了解到,在继承抽象类时,我应该使用extends关键字而不是工具,因为dart告诉我也必须实现非抽象方法,因为当需要时,所有方法都必须实现使用接口。我认为我的问题是

© www.soinside.com 2019 - 2024. All rights reserved.