“扩展”与“实施”的对比与“带有”相对 [英] "extends" versus "implements" versus "with"

查看:70
本文介绍了“扩展”与“实施”的对比与“带有”相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解扩展实现。何时使用每个关键字?

I want to understand the difference between extends, implements and with. When do I use each keyword?

推荐答案

扩展:


使用扩展来创建

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

扩展是典型的子类 OOP 类继承。如果 a 类扩展了 b 类,则在 a 类中也可以使用在 b 类中实现的所有属性,变量和函数。 。另外,您可以覆盖函数等。

Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.

如果要创建类的更特定版本,请使用 extend 。例如,汽车类可以扩展车辆类。在Dart中,一个班级只能扩展一个班级。

You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.

实现:


每个类都隐式定义一个包含类的所有实例成员及其实现的任何接口的接口。如果要创建一个支持类B的API而不继承其B的实现的类A,则类A应该实现B接口。

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.

<如果要创建自己的另一个类或接口的实现,可以使用code> Implements 。当类 a 实现类 b 时。必须实现在类 b 中定义的所有功能。

Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.

在实现另一个类时,您不会从该类继承代码。您仅继承类型。在Dart中,您可以将 implements 关键字用于多个类或接口。

When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.

使用(Mixins):


Mixins是在多个类层次结构中重用类代码的一种方式。

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

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

With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.

在Flutter中使用它们来包含常见的代码片段。常见的Mixin是 SingleTickerProviderStateMixin

They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.

这篇关于“扩展”与“实施”的对比与“带有”相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆