飞镖之后的构造函数 [英] Colon after Constructor in dart
问题描述
此代码来自flutter画廊,我正在尝试理解和改编它。我会知道此语法的含义:
This code is from flutter gallery and i'm trying to understanding and adapting it. I would know what this syntax means:
class DemoItem<T> {
DemoItem({
this.valueName,
this.hintName,
this.valueSurname,
this.hintSurname,
this.builder,
this.valueToString
}) : textController = new TextEditingController(text: valueToString(valueName));
特别是,我会知道在构造函数之后的冒号是什么,以及是否存在
Especially i would know what means the colon after the constructor and if there is a way to define another TextEditingController, in addition to the one already defined.
推荐答案
之后的部分是一种定义另一个TextEditingController的方法。 :
称为初始化列表。这是一个,
分隔的表达式列表,可以访问构造函数参数并可以分配给实例字段,甚至 final
实例字段,这很方便初始化带有计算值的最终字段。
The part after :
is called "initializer list. It is a ,
-separated list of expressions that can access constructor parameters and can assign to instance fields, even final
instance fields. This is handy to initialize final fields with calculated values.
初始化程序列表也可用于调用其他构造函数,例如:...,super('foo')
。
The initializer list is also used to call other constructors like : ..., super('foo')
.
自Dart 1.24版开始list还支持 assert(...)
,它很方便检查参数值。
Since about Dart version 1.24 the initializer list also supports assert(...)
which is handy to check parameter values.
初始化列表可以请从 this
中读取,因为超级构造函数需要在访问 this
有效之前完成,但它可以分配给 this.xxx
。
The initializer list can't read from this
because the super constructors need to be completed before access to this
is valid, but it can assign to this.xxx
.
根据user693336的注释中的说明指出:
Pointing out as mentioned in the comments by user693336:
这也意味着初始化列表在构造函数体之前执行。同样,所有超类的初始化列表都在执行任何构造器主体之前执行。
This also means the initializer list is executed before the constructor body. Also the initializer lists of all superclasses are executed before any of the contructor bodies are executed.
这篇关于飞镖之后的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!