我想了解 extends
之间的区别, implements
和 with
.我什么时候使用每个关键字?
最佳答案
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/