flutter - 重写方法以向其添加功能

标签 flutter dart

假设我们有:

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/

相关文章:

flutter - 如何将appState(提供者)传递到initState的屏幕中?

flutter - 在对话框中使用 BLoC : Could not find the correct Provider

asynchronous - 关于Dart async/await。我使用一段代码进行测试,但是打印结果与预期不符

dart - 带有容器高度的图标上的Flutter TextFormField

google-maps - Flutter Google Maps - 根据行车方向旋转标记

flutter - Flutter源码中Te​​xt widget的RenderObject类在哪里

gridview - flutter - 在 gridview 上换行时如何设置宽度?

listview - 我可以使用最初未声明的itemCount定义ListView Builder吗

dart - flutter 和 Material 设计 2

android - Flutter - 如何获取 TextField 值并将其显示为新文本