dart - "extends"与 "implements"与 "with"

标签 dart

我想了解 extends 之间的区别, implementswith .我什么时候使用每个关键字?

最佳答案

Extends:

Use extends to create a subclass, and super to refer to the superclass.


Extends是典型的OOP类继承。如果类一个 扩展类 b 在类 中实现的所有属性、变量、函数b 也可在 类中使用一个 .此外,您可以覆盖功能等。

您使用 extend如果你想创建一个更具体的类版本。例如类 汽车可以扩展类 车辆 .在 Dart 中,一个类只能扩展一个类。

Implements:

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.


Implements如果您想创建自己的另一个类或接口(interface)的实现,可以使用。上课时一个 实现类 b .类 中定义的所有函数b 必须实现。

当您实现另一个类时,您不会从该类继承代码。你只继承类型。在 Dart 中,您可以使用 implements具有多个类或接口(interface)的关键字。

With (Mixins):

Mixins are a way of reusing a class’s code in multiple class hierarchies.


With用于包含 Mixins。 mixin 是一种不同类型的结构,只能与关键字 with 一起使用.

它们在 Flutter 中用于包含常见的代码片段。一个常用的 Mixin 是 SingleTickerProviderStateMixin .

关于dart - "extends"与 "implements"与 "with",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55295782/

相关文章:

Flutter 后退按钮隐藏在导航中

dart - 在 Dart/Flutter 中从集合转换为列表?

android - Flutter 如何使用 AssetBundle 存储和访问文件

flutter - 如何发现Flutter中的内存泄漏?

json - 使用 JSON 序列化/反序列化 Dart 的新枚举的最佳方法是什么?

flutter - 解码JSON抖动

android - Flutter 中使用 dart 的视频编辑器

dart - 未调用异步方法

dart - Dart-未为 'someVar'类定义二传手 'Map'

dart - 如何在 Dart 中模拟 protected 方法?