简体   繁体   English

Dart 命名构造函数、static 方法和工厂构造函数

[英]Dart named constructor, static method, and factory constructor

Given the following code:给定以下代码:

const jsonString = '{"myString":"Hello"}';
final jsonMap = jsonDecode(jsonString);

final myObject = MyClass.fromJson(jsonMap);

How many ways are there to create a new object using this syntax:使用以下语法创建新的 object 有多少种方法:

MyClass.fromJson(jsonMap)

Recently I've been trying to understand the differences between named constructors, factory constructors and static methods so I'm posting my answer below so that I have something to come back to as a reference in the future.最近,我一直在尝试了解命名构造函数、工厂构造函数和 static 方法之间的区别,所以我在下面发布我的答案,以便将来有一些东西可以作为参考。

To create a new instance of an object using the following syntax:使用以下语法创建 object 的新实例:

MyClass.fromJson(jsonMap)

For use with the following code:与以下代码一起使用:

// import 'dart:convert';

const jsonString = '{"myString":"Hello"}';
final jsonMap = jsonDecode(jsonString);

final myObject = MyClass.fromJson(jsonMap);

There are at least the following ways to do it (with supplemental notes about the characteristics of each):至少有以下几种方法(附有关于每种特征的补充说明):

Generative constructor生成式构造函数

class MyClass {
  MyClass(this.myString);
  final String myString;

  MyClass.fromJson(Map<String, dynamic> json) : this(json['myString']);
}

There are two kinds of generative constructors: named and unnamed.有两种生成构造函数:命名的和未命名的。 The MyClass.fromJson() is a named constructor while MyClass() is an unnamed constructor. MyClass.fromJson()是命名构造函数,而MyClass()是未命名构造函数。 The following principles apply to generative constructors:以下原则适用于生成构造函数:

  • Generative constructors may only instantiate the class itself.生成式构造函数只能实例化 class 本身。
  • Generative constructors can use an initializer list.生成式构造函数可以使用初始化列表。
  • Generative constructors may only use initializing parameters or the initializer list to set final properties, that is, not in the constructor body.生成式构造函数只能使用初始化参数或初始化列表来设置final属性,也就是说,不能在构造函数体中。
  • Generative constructors can be const , even if they are not redirecting.生成式构造函数可以是const ,即使它们没有重定向。

Factory constructor工厂建设者

class MyClass {
  MyClass(this.myString);
  final String myString;

  factory MyClass.fromJson(Map<String, dynamic> json) {
    return MyClass(json['myString']);
  }
}
  • Factory constructors may return a subtype of the class.工厂构造函数可能返回 class 的子类型。
  • Factory constructors can be used to create singletons.工厂构造函数可用于创建单例。
  • Factory constructors can be unnamed like generative constructors.工厂构造函数可以像生成构造函数一样不命名。
  • Factory constructors can be const , but only when redirecting.工厂构造函数可以是const ,但仅限于重定向时。

Static method Static方法

class MyClass {
  MyClass(this.myString);
  final String myString;

  static MyClass fromJson(Map<String, dynamic> json) {
    return MyClass(json['myString']);
  }
}
  • Static methods may return anything, including a Future. Static 方法可以返回任何东西,包括 Future。
  • Static methods can be used to create singletons. Static 方法可用于创建单例。
  • Static methods can be used as tear-offs. Static 方法可用作撕下。

Further reading进一步阅读

In addition to @suragch detailed answer.除了@suragch 的详细答案。 I like to give some bullet points that show factory constructor is the best option for the above scenario (for fromJson() method).我想给出一些要点,表明factory constructor是上述场景的最佳选择(对于 fromJson() 方法)。

  • When using factory constructors, you don't need to initialize instance variables of that class.使用工厂构造函数时,您不需要初始化该 class 的实例变量。 (but when you using generative constructors, need to initialize all the final instance variables) (但是当您使用生成构造函数时,需要初始化所有最终实例变量)

  • Factory constructor can return an existing object.工厂构造函数可以返回一个现有的 object。 Eg:- when using json_seriazible package, fromJson() method return an existing (previously made) object.例如:- 当使用 json_seriazible package 时,fromJson() 方法返回一个现有的(以前制作的)object。 So we can only use factory constructors with this package.所以我们只能使用带有这个 package 的工厂构造函数。

  • Factory constructors can return any subtype of that class, but when using generative constructors, it can only return the exact type object of that class.工厂构造函数可以返回 class 的任何子类型,但是当使用生成构造函数时,它只能返回 class 的确切类型 object。

  • Ensures only one instance of a class is ever created (singleton pattern).确保只创建一个 class 实例(单例模式)。 (object are expensive, so singleton pattern should needed for fromJson) (对象很昂贵,因此 fromJson 应该需要 singleton 模式)

According to the above points, we can see generative constructors add more limitations for fromJson constructor and static methods give fewer limitations for fromJson so it can cause type errors by returning different type objects.根据以上几点,我们可以看到生成式构造函数为 fromJson 构造函数添加了更多限制,而 static 方法对 fromJson 的限制更少,因此它可能通过返回不同类型的对象而导致类型错误。

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

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