跳到主要内容

构造函数

构造函数是用于创建类实例的特殊函数。

Dart 实现了多种类型的构造函数。除了默认构造函数外,这些函数的名称与类名相同。

生成式构造函数
创建新实例并初始化实例变量。
默认构造函数
当没有指定构造函数时,用于创建新实例。它不接受参数且没有名称。
命名构造函数
阐明构造函数的目的,或允许为同一类创建多个构造函数。
常量构造函数
创建作为编译时常量的实例。
工厂构造函数
创建子类型的新实例,或者从缓存返回现有实例。
重定向构造函数
将调用转发到同一类的另一个构造函数。

构造函数类型

#

生成式构造函数

#

要实例化一个类,使用生成式构造函数。

dart
class Point {
  // Instance variables to hold the coordinates of the point.
  double x;
  double y;

  // Generative constructor with initializing formal parameters:
  Point(this.x, this.y);
}

默认构造函数

#

如果你没有声明构造函数,Dart 会使用默认构造函数。默认构造函数是没有参数或名称的生成式构造函数。

命名构造函数

#

使用命名构造函数可以为一个类实现多个构造函数,或提供额外的清晰度

dart
const double xOrigin = 0;
const double yOrigin = 0;

class Point {
  final double x;
  final double y;

  // Sets the x and y instance variables
  // before the constructor body runs.
  Point(this.x, this.y);

  // Named constructor
  Point.origin() : x = xOrigin, y = yOrigin;
}

子类不继承超类的命名构造函数。要在子类中创建超类中定义的命名构造函数,请在子类中实现该构造函数。

常量构造函数

#

如果你的类产生不可变的对象,请将这些对象设为编译时常量。要使对象成为编译时常量,请定义一个 const 构造函数,并将所有实例变量设置为 final

dart
class ImmutablePoint {
  static const ImmutablePoint origin = ImmutablePoint(0, 0);

  final double x, y;

  const ImmutablePoint(this.x, this.y);
}

常量构造函数并非总是创建常量。它们可能在非 const 上下文中使用。要了解更多信息,请查阅使用构造函数一节。

重定向构造函数

#

构造函数可以将调用重定向到同一类的另一个构造函数。重定向构造函数没有函数体。构造函数在冒号 (:) 后使用 this 而不是类名。

dart
class Point {
  double x, y;

  // The main constructor for this class.
  Point(this.x, this.y);

  // Delegates to the main constructor.
  Point.alongXAxis(double x) : this(x, 0);
}

工厂构造函数

#

在实现构造函数时遇到以下两种情况之一时,使用 factory 关键字

  • 构造函数并不总是创建其类的新实例。尽管工厂构造函数不能返回 null,但它可能会返回

    • 缓存中的现有实例,而不是创建新实例
    • 子类的新实例
  • 在构造实例之前需要执行非平凡的工作。这可能包括检查参数或执行初始化列表中无法处理的任何其他处理。

以下示例包含两个工厂构造函数。

  • Logger 工厂构造函数从缓存返回对象。
  • Logger.fromJson 工厂构造函数根据 JSON 对象初始化一个 final 变量。
dart
class Logger {
  final String name;
  bool mute = false;

  // _cache is library-private, thanks to
  // the _ in front of its name.
  static final Map<String, Logger> _cache = <String, Logger>{};

  factory Logger(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }

  factory Logger.fromJson(Map<String, Object> json) {
    return Logger(json['name'].toString());
  }

  Logger._internal(this.name);

  void log(String msg) {
    if (!mute) print(msg);
  }
}

像使用其他构造函数一样使用工厂构造函数

dart
var logger = Logger('UI');
logger.log('Button clicked');

var logMap = {'name': 'UI'};
var loggerJson = Logger.fromJson(logMap);

重定向工厂构造函数

#

重定向工厂构造函数指定了对同一类中另一个构造函数的调用,每当调用该重定向构造函数时都会使用此调用。

dart
factory Listenable.merge(List<Listenable> listenables) = _MergingListenable

普通工厂构造函数似乎可以创建并返回其他类的实例。这可能会使重定向工厂变得不必要。重定向工厂有几个优点

  • 抽象类可以提供一个常量构造函数,它使用另一个类的常量构造函数。
  • 重定向工厂构造函数避免了转发器重复形式参数及其默认值的需要。

构造函数截取(tear-off)

#

Dart 允许你将构造函数作为参数提供,而无需调用它。这称为 截取(因为你 截取 了括号),它充当一个闭包,使用相同的参数调用构造函数。

如果截取是一个构造函数,其签名和返回类型与方法接受的相同,你可以将该截取用作参数或变量。

截取与 lambda 或匿名函数不同。lambda 充当构造函数的包装器,而截取就是构造函数本身。

使用截取

gooddart
// Use a tear-off for a named constructor:
var strings = charCodes.map(String.fromCharCode);

// Use a tear-off for an unnamed constructor:
var buffers = charCodes.map(StringBuffer.new);

而不是 Lambda

baddart
// Instead of a lambda for a named constructor:
var strings = charCodes.map((code) => String.fromCharCode(code));

// Instead of a lambda for an unnamed constructor:
var buffers = charCodes.map((code) => StringBuffer(code));

要了解更多讨论,请观看关于截取的这个 Decoding Flutter 视频。

在新标签页中在 YouTube 上观看:“Dart Tear-offs | Decoding Flutter”

实例变量初始化

#

Dart 可以通过三种方式初始化变量。

在声明中初始化实例变量

#

在声明变量时初始化实例变量。

dart
class PointA {
  double x = 1.0;
  double y = 2.0;

  // The implicit default constructor sets these variables to (1.0,2.0)
  // PointA();

  @override
  String toString() {
    return 'PointA($x,$y)';
  }
}

使用初始化形式参数

#

为了简化将构造函数参数赋值给实例变量的常见模式,Dart 引入了 初始化形式参数

在构造函数声明中,包含 this.<propertyName> 并省略函数体。this 关键字指代当前实例。

当存在名称冲突时,使用 this。否则,Dart 风格省略 this。生成式构造函数有一个例外,你必须以 this 为前缀初始化形式参数名称。

如本指南前面所述,某些构造函数和构造函数的某些部分无法访问 this。其中包括

  • 工厂构造函数
  • 初始化列表的右侧
  • 传递给超类构造函数的参数

初始化形式参数还允许你初始化不可为空或 final 的实例变量。这两种类型的变量都需要初始化或默认值。

dart
class PointB {
  final double x;
  final double y;

  // Sets the x and y instance variables
  // before the constructor body runs.
  PointB(this.x, this.y);

  // Initializing formal parameters can also be optional.
  PointB.optional([this.x = 0.0, this.y = 0.0]);
}

私有字段不能用作命名初始化形式参数。

dart
class PointB {
// ...

  PointB.namedPrivate({required double x, required double y})
      : _x = x,
        _y = y;

// ...
}

这也适用于命名变量。

dart
class PointC {
  double x; // must be set in constructor
  double y; // must be set in constructor

  // Generative constructor with initializing formal parameters
  // with default values
  PointC.named({this.x = 1.0, this.y = 1.0});

  @override
  String toString() {
    return 'PointC.named($x,$y)';
  }
}

// Constructor using named variables.
final pointC = PointC.named(x: 2.0, y: 2.0);

通过初始化形式参数引入的所有变量都是 final 的,并且仅在其初始化变量的作用域内。

要执行无法在初始化列表中表达的逻辑,请创建包含该逻辑的工厂构造函数静态方法。然后,你可以将计算出的值传递给普通构造函数。

构造函数参数可以设置为可为空且不进行初始化。

dart
class PointD {
  double? x; // null if not set in constructor
  double? y; // null if not set in constructor

  // Generative constructor with initializing formal parameters
  PointD(this.x, this.y);

  @override
  String toString() {
    return 'PointD($x,$y)';
  }
}

使用初始化列表

#

在构造函数体运行之前,你可以初始化实例变量。使用逗号分隔初始化器。

dart
// Initializer list sets instance variables before
// the constructor body runs.
Point.fromJson(Map<String, double> json) : x = json['x']!, y = json['y']! {
  print('In Point.fromJson(): ($x, $y)');
}

在开发期间验证输入时,在初始化列表中使用 assert

dart
Point.withAssert(this.x, this.y) : assert(x >= 0) {
  print('In Point.withAssert(): ($x, $y)');
}

初始化列表有助于设置 final 字段。

以下示例在初始化列表中初始化了三个 final 字段。要执行代码,请点击 运行

import 'dart:math';

class Point {
  final double x;
  final double y;
  final double distanceFromOrigin;

  Point(double x, double y)
    : x = x,
      y = y,
      distanceFromOrigin = sqrt(x * x + y * y);
}

void main() {
  var p = Point(2, 3);
  print(p.distanceFromOrigin);
}

构造函数继承

#

子类(或子类)不继承其超类(或直接父类)的构造函数。如果一个类没有声明构造函数,它只能使用默认构造函数

类可以继承超类的参数。这些参数称为超参数

构造函数的工作方式与调用一系列静态方法有些类似。每个子类都可以调用其超类的构造函数来初始化实例,就像子类可以调用超类的静态方法一样。这个过程不会“继承”构造函数体或签名。

非默认超类构造函数

#

Dart 按照以下顺序执行构造函数

  1. 初始化列表
  2. 超类的未命名、无参数构造函数
  3. 主类的无参数构造函数

如果超类没有未命名、无参数构造函数,请调用超类中的一个构造函数。在构造函数体(如果有)之前,在冒号 (:) 后指定超类构造函数。

在以下示例中,Employee 类的构造函数调用其超类 Person 的命名构造函数。要执行以下代码,请点击 运行

class Person {
  String? firstName;

  Person.fromJson(Map data) {
    print('in Person');
  }
}

class Employee extends Person {
  // Person does not have a default constructor;
  // you must call super.fromJson().
  Employee.fromJson(Map data) : super.fromJson(data) {
    print('in Employee');
  }
}

void main() {
  var employee = Employee.fromJson({});
  print(employee);
  // Prints:
  // in Person
  // in Employee
  // Instance of 'Employee'
}

由于 Dart 在调用超类构造函数之前评估传递给它的参数,因此参数可以是函数调用等表达式。

dart
class Employee extends Person {
  Employee() : super.fromJson(fetchDefaultData());
  // ···
}

超参数

#

为了避免将每个参数传递给构造函数的超类调用,使用超初始化参数将参数转发到指定的或默认的超类构造函数。你不能将此特性与重定向构造函数一起使用。超初始化参数的语法和语义与初始化形式参数类似。

如果超类构造函数调用包含位置参数,则超初始化参数不能是位置参数。

dart
class Vector2d {
  final double x;
  final double y;

  Vector2d(this.x, this.y);
}

class Vector3d extends Vector2d {
  final double z;

  // Forward the x and y parameters to the default super constructor like:
  // Vector3d(final double x, final double y, this.z) : super(x, y);
  Vector3d(super.x, super.y, this.z);
}

为了进一步说明,请看以下示例。

dart
  // If you invoke the super constructor (`super(0)`) with any
  // positional arguments, using a super parameter (`super.x`)
  // results in an error.
  Vector3d.xAxisError(super.x): z = 0, super(0); // BAD

这个命名构造函数试图两次设置 x 的值:一次在超类构造函数中,一次作为位置超参数。由于两者都指向 x 位置参数,这会导致错误。

当超类构造函数有命名参数时,你可以在命名超参数(下例中的 super.y)和超类构造函数调用的命名参数(super.named(x: 0))之间分配它们。

dart
class Vector2d {
  // ...
  Vector2d.named({required this.x, required this.y});
}

class Vector3d extends Vector2d {
  final double z;

  // Forward the y parameter to the named super constructor like:
  // Vector3d.yzPlane({required double y, required this.z})
  //       : super.named(x: 0, y: y);
  Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);
}