假设我们有:
class Human{
String name;
Human(this.name);
void printMyInfo(){
print("My name is ${this.name}");
}
}
class Bob extends Human{
int age;
Bob(this.name, this.age) : super(name);
}
有没有一种方法可以只添加以下行
print("My age is ${this.age}");
继承printMyInfo()方法而不重写以前的代码?
我的目标是运行以获得以下输出:
My name is *;
My age is *;
通过仅添加最后的打印方法
最佳答案
class Bob extends Human{
int age;
Bob(this.name, this.age) : super(name);
void printMyInfo(){
super.printMyInfo();
print("My age is $age");
}
}
关于flutter - 重写方法以向其添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62332161/