当前位置:网站首页 > 技术博客 > 正文

构造器和方法



¥Description

构造函数使你能够提供在实例化对象上调用任何其他方法之前必须完成的任何自定义初始化。

¥A constructor enables you to provide any custom initialization that must be done before any other methods can be called on an instantiated object.

如果你不提供自己的构造函数,那么将为你提供默认构造函数。如果你的类是基类,则默认构造函数为空:

¥If you don't provide your own constructor, then a default constructor will be supplied for you. If your class is a base class, the default constructor is empty:

如果你的类是派生类,则默认构造函数将调用父构造函数,并传递提供的任何参数:

¥If your class is a derived class, the default constructor calls the parent constructor, passing along any arguments that were provided:

注意:像上面这样的显式构造函数和默认构造函数之间的区别在于,后者实际上并不调用 数组迭代器 到 争论蔓延。

¥Note: The difference between an explicit constructor like the one above and the default constructor is that the latter doesn't actually invoke the array iterator through argument spreading.

这使得这样的代码能够工作:

¥That enables code like this to work:

类不需要显式构造函数,因为它不需要执行任何自定义初始化。然后,默认构造函数负责根据给定的参数初始化父级 。

¥The class doesn't need an explicit constructor, because it doesn't need to do any custom initialization. The default constructor then takes care of initializing the parent from the argument it is given.

但是,如果你提供自己的构造函数,并且你的类派生自某个父类,则必须使用 显式调用父类构造函数。例如:

¥However, if you provide your own constructor, and your class derives from some parent class, then you must explicitly call the parent class constructor using . For example:

在类上使用 需要经过以下步骤:

¥Using on a class goes through the following steps:

  1. (如果是派生类)评估 调用之前的 主体。该部分不应访问 ,因为它尚未初始化。
  2. (如果是派生类)评估 调用,它通过相同的过程初始化父类。
  3. 当前类的 fields 已初始化。
  4. 评估 调用后的 主体(或整个主体,如果它是基类)。

在 主体内,你可以访问通过 创建的对象,并访问通过 到 调用的类。请注意,在执行 之前,方法(包括 getters 和 setters)和 原型链 已经在 上初始化,因此你甚至可以从超类的构造函数访问子类的方法。但是,如果这些方法使用 ,则 将尚未完全初始化。这意味着读取派生类的公共字段将导致 ,而读取私有字段将导致 。

¥Within the body, you can access the object being created through and access the class that is called with through . Note that methods (including getters and setters) and the prototype chain are already initialized on before the is executed, so you can even access methods of the subclass from the constructor of the superclass. However, if those methods use , the will not have been fully initialized yet. This means reading public fields of the derived class will result in , while reading private fields will result in a .

方法可以有返回值。虽然基类可以从其构造函数返回任何内容,但派生类必须返回一个对象或 ,否则将抛出 。

¥The method may have a return value. While the base class may return anything from its constructor, the derived class must return an object or , or a will be thrown.

如果父类构造函数返回一个对象,则该对象将用作定义派生类的 类字段 的 值。这个技巧称为 "返回覆盖",它允许派生类的字段(包括 private 字段)在不相关的对象上定义。

¥If the parent class constructor returns an object, that object will be used as the value on which class fields of the derived class will be defined. This trick is called "return overriding", which allows a derived class's fields (including private ones) to be defined on unrelated objects.

遵循正常的 method 语法,因此 参数默认值、其余参数 等都可以使用。

¥The follows normal method syntax, so parameter default values, rest parameters, etc. can all be used.

构造函数必须是字面名称。计算属性 不能成为构造函数。

¥The constructor must be a literal name. Computed properties cannot become constructors.

禁止调用异步方法、生成器方法、访问器和类字段 。私有名字不能称为 。任何名为 的成员都必须是普通方法。

¥Async methods, generator methods, accessors, and class fields are forbidden from being called . Private names cannot be called . Any member named must be a plain method.

  • 上一篇: ip工具是什么意思
  • 下一篇: http代理 dns
  • 版权声明


    相关文章:

  • ip工具是什么意思2025-04-08 10:30:00
  • json.cpp2025-04-08 10:30:00
  • rbf 神经网络2025-04-08 10:30:00
  • java课程设计手机通讯录2025-04-08 10:30:00
  • 安装软件2503错误怎么解决2025-04-08 10:30:00
  • http代理 dns2025-04-08 10:30:00
  • 企业微信监控系统2025-04-08 10:30:00
  • dbn-bp2025-04-08 10:30:00
  • leveldb lrucache2025-04-08 10:30:00
  • 常见的模型评估指标有哪些2025-04-08 10:30:00