简体   繁体   English

Dart:Dart 中的实现和扩展有什么区别

[英]Dart: What is the difference between implements and extends in Dart

https://dart.dev/guides/language/language-tour#implicit-interfaces https://dart.dev/guides/language/language-tour#implicit-interfaces

I've seen code that uses "implements", so I'm looking into "implements".我见过使用“实现”的代码,所以我正在研究“实现”。 But I can't really tell the difference from extends by looking at the official docs.但是通过查看官方文档,我无法真正分辨出与扩展的区别。

Looking at the sample code in the official documentation (page above), it looks like it is just doing what it can do with extends with implements.查看官方文档(上一页)中的示例代码,看起来它只是在做它可以用扩展实现的功能。 Then I wonder if it should use "extends".然后我想知道它是否应该使用“扩展”。 I think I've understood about inheritance (extends) and mixins (with) so far.我想到目前为止我已经了解了继承(扩展)和混合(与)。 The word "interface" is also mentioned in the Dart documentation, but there is a clear definition of "interface". Dart文档中也提到了“接口”这个词,但是对“接口”有明确的定义。 I can't even find where it is.我什至找不到它在哪里。 I don't have much knowledge about interfaces in other languages, so it's hard to get an image.我对其他语言的接口了解不多,所以很难得到图像。

What exactly is the difference between inheritance and implementation?继承和实现之间究竟有什么区别? Because of the difference, when do you use the inheritance "extends" and when do you use the implementation "implements"?因为区别,什么时候用继承“extends”,什么时候用实现“implements”?

Is there somewhere in the sample that makes a clear difference?样本中是否有明显不同的地方?

Given class MyClass :给定类MyClass

  • MyClass extends Foo is classic inheritance. MyClass extends Foo是经典的继承。 MyClass is a Foo MyClass是一个Foo
  • MyClass implements Bar is declaring that the implementer conforms to the Bar interface. MyClass implements Bar声明实现者符合Bar接口。 MyClass "looks" like a Bar MyClass “看起来”像一个Bar
  • MyClass with Batz is "mixing" in the interface and implementation. MyClass with Batz在接口和实现中是“混合”的。 MyClass "acts" like a Batz . MyClass “行为”就像一个Batz

MyClass can implement and mixin as many interfaces as needed (some limitations apply) but can only extend from one interface. MyClass可以根据需要实现和混合尽可能多的接口(有一些限制),但只能从一个接口扩展。

Besides other explanations, dart does not let developers to use multiple inheritance so there can be only one extends , however there can be more than one implements .除了其他解释外,dart 不允许开发人员使用多重继承,因此可以只有一个extends ,但是可以有多个implements
Therefore a class can be subtype of only one class(except parents of parent class) but interfaces defines behaviour of the class which implements it and one class can have different behaviours.因此,一个类只能是一个类的子类型(父类的父类除外),但接口定义了实现它的类的行为,一个类可以有不同的行为。

A simple analogy is from animals.一个简单的类比来自动物。 If we assume there is different behaviours for animals like swimming interface as ISwimmer, running interface as IRunner and flying interface as IFlyer.如果我们假设动物有不同的行为,例如游泳界面为 ISwimmer,运行界面为 IRunner,飞行界面为 IFlyer。 For example a Fish class is an Animal.例如,鱼类动物。 Which makes it extends Animal class.这使得它扩展了 Animal 类。 Additionally a Fish has behaviour of swimming so it implements ISwimmer interface in that purpose.此外,鱼具有游泳行为,因此它为此目的实现了 ISwimmer 接口。

If you just want to understand how they are different concept-wise then,如果您只想了解它们在概念上的不同之处,那么

You extend a class.你扩展一个类。 Think of it as you extended your father or your father is your base class.当您扩展您的父亲或您的父亲是您的基类时,可以考虑一下。

You implement an interface(which is just a purely abstract class).您实现了一个接口(这只是一个纯粹的抽象类)。 Like Ferrari implements a car.就像法拉利实施汽车一样。

An interface cannot have an instance.接口不能有实例。 For example - Have you seen any CAR?例如 - 你见过任何汽车吗? The answer is no, you have seen types of car ie, Ford, Toyota which implements the CAR so the car acts as an interface which other companies or you can say, classes(Ferrari) implements.答案是否定的,您已经看到了实现 CAR 的汽车类型,即福特、丰田,因此汽车充当其他公司或您可以说类(法拉利)实现的接口。

You have to implement every feature of a car to be called a car that's why every method of an interface needs to be implemented and we say x "implements" y.你必须实现汽车的每一个特性才能被称为汽车,这就是为什么接口的每个方法都需要实现,我们说 x “实现” y。

In extending you can override something and skip another , suppose your nose may look like your father but your ears do not.在扩展中,您可以覆盖某些内容并跳过另一个内容,假设您的鼻子可能看起来像您的父亲,但您的耳朵却不是。

Think of an interface as a skeleton or just an empty class.将接口视为骨架或只是一个空类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM