我搞不懂我们为什么需要工具?根据这https://dart.dev/language/classes#implicit-interfaces条 在我们使用了实现之后,我们应该重写父类中除了构造函数之外的所有内容.

// A person. The implicit interface contains greet().
class Person {
  // In the interface, but visible only in this library.
  final String _name;

  // Not in the interface, since this is a constructor.
  Person(this._name);

  // In the interface.
  String greet(String who) => 'Hello, $who. I am $_name.';
}

// An implementation of the Person interface.
class Impostor implements Person {
  String get _name => '';

  String greet(String who) => 'Hi $who. Do you know who I am?';
}

所以我的问题是,为什么我们不能只创建一个新类,而不是使用工具呢?

推荐答案

使用Derived implements Base的目的是指定DerivedBase符合相同的接口.在需要Base对象的情况下,Derived等于substitutable.如果您创建了一个新的、不相关的类,那么类型系统将阻止您将该类的实例作为Base传递.(在没有静态类型的语言中,您不需要implements之类的内容,因为您可以使用duck typing.如果您真的愿意,如果您使用dynamic,您也可以在DART中这样做.)

extends不同,implements允许一个类提供multiple个不相关的接口,而不是the ambiguity that can come from true multiple inheritance个.

Dart相关问答推荐

捕获一个区域中的所有期货

将 Stream> 转换为 Stream

如何在flutter中管理switch小部件的原生外观

如何从 Flutter App 连接 Ms SQL?

Flutter-创建value和name数组

如何签署 Flutter 的应用程序

在Flatter中滚动列表视图时,网络图像一直消失?

没有 AppBar 的 Flutter 应用设计

有没有更好的方法来解析 Dart 中的 int

在Flutter 中使用 import 'dart:html' - 我需要额外的依赖项吗?

Flutter 中 ChangeNotifier 的构建器小部件

无法使用 Flutter 调用 Localhost,将随机端口分配给 HTTP GET 调用

使用 dart 下载文件

在 Dart 中调用异步函数而不等待,例如启动一个线程

一个集合如何确定两个对象在dart中相等?

为什么不推荐使用 context2d.backingStorePixelRatio?

如何在 Dart 2 中将 List 更改为 List

有 Javascript 到 Dart 的转换器吗?

如何将多个 Stream 合并为更高级别的 Stream?

如何在 Dart 中创建私有变量?