我正在尝试抽象类,发现一个问题,我必须实现在子类中具有主体的非抽象方法
代码:
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方法,但它给出了错误并指出必须实现抽象方法。
是此错误还是我弄错了概念
我不认为这是一个错误。该方法仍在您正在[[implementing的抽象类中。我认为,您打算改为extend该类,在这种情况下,您将在覆盖中调用super.makeNoise()
。
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");
}
}
傻