[英]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):至少有以下几种方法(附有关于每种特征的补充说明):
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:以下原则适用于生成构造函数:
final
properties, that is, not in the constructor body.final
属性,也就是说,不能在构造函数体中。const
, even if they are not redirecting.const
,即使它们没有重定向。class MyClass {
MyClass(this.myString);
final String myString;
factory MyClass.fromJson(Map<String, dynamic> json) {
return MyClass(json['myString']);
}
}
const
, but only when redirecting.const
,但仅限于重定向时。class MyClass {
MyClass(this.myString);
final String myString;
static MyClass fromJson(Map<String, dynamic> json) {
return MyClass(json['myString']);
}
}
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.