Flutter 6種構(gòu)造函數(shù)詳解

Flutter有生成構(gòu)造函數(shù)、默認(rèn)構(gòu)造函數(shù)、命名構(gòu)造函數(shù)、重定向構(gòu)造函數(shù)、常量構(gòu)造函數(shù)、工廠構(gòu)造函數(shù)

一.生成構(gòu)造函數(shù)

生成構(gòu)造函數(shù)是最常見的構(gòu)造函數(shù),即生成實(shí)體類對(duì)象。

class Point {
  num x, y; //Dart中int和double是num的子類

  //this引用當(dāng)前類對(duì)象 
  Point(num x, num y) {
    this.x = x;
    this.y = y;
  }
//也可以用語法糖
  Point(this.x, this.y);
}
二.默認(rèn)構(gòu)造函數(shù)

如果未聲明構(gòu)造函數(shù),則會(huì)提供默認(rèn)構(gòu)造函數(shù)。 默認(rèn)構(gòu)造函數(shù)沒有參數(shù),并調(diào)用父類無參數(shù)構(gòu)造函數(shù)。

class Parent{
  Parent(){
    print('In Parent\'s constructor.');
  }
}

class Child extends Parent{
  Child(){
    print('In Child\'s constructor.');
  }
}

//new Child(); -> 打印 In Parent's constructor.  In Child's constructor.

默認(rèn)情況下,子類中的構(gòu)造函數(shù)調(diào)用父類的未命名無參數(shù)構(gòu)造函數(shù)。 父類的構(gòu)造函數(shù)在子類構(gòu)造函數(shù)體的開頭被調(diào)用。 如果還使用初始化了列表,則會(huì)在調(diào)用父類構(gòu)造函數(shù)之前執(zhí)行。 執(zhí)行順序如下:

初始化列表
父類的無參數(shù)構(gòu)造函數(shù)
子類的無參數(shù)構(gòu)造函數(shù)

如果父類沒有未命名的無參數(shù)構(gòu)造函數(shù),則必須手動(dòng)調(diào)用父類中的一個(gè)構(gòu)造函數(shù)。 在子類的構(gòu)造函數(shù)體之后用冒號(hào)(:)指定父類構(gòu)造函數(shù)

class Parent{
  num x;
  num y;
  Parent(this.x, this.y){
    print('In Parent\'s constructor.');
  }
}

class Child extends Parent{
  Child(num x, num y) : super(x, y){
    print('In Child\'s constructor.');
  }
}

//new Child(100, 100); -> 打印
//In Parent's constructor.
//In Child's constructor.
三.命名構(gòu)造函數(shù)

當(dāng)需要定義一個(gè)有特別含義的構(gòu)造函數(shù)的時(shí)候,可以通過命名構(gòu)造 形式:構(gòu)造函數(shù).XXX來命名構(gòu)造函數(shù)

class Point{
num x;
num y;

Point(this.x, this.y);

//創(chuàng)建一個(gè)坐標(biāo)原點(diǎn)類
Point.origin(){
  this.x = 0;
  this.y = 0;
}

//創(chuàng)建一個(gè)坐標(biāo)為(100, 100)的類
Point.coordinate100(){
  this.x = 100;
  this.y = 100;
}
@override
String toString() {
  return ("x: $x, y: $y");
}
}
四.重定向構(gòu)造函數(shù)

有時(shí)構(gòu)造函數(shù)需要重定向到同一個(gè)類中的另一個(gè)構(gòu)造函數(shù),在冒號(hào)后面用this:

class Point {
  num x, y;

  //類的主構(gòu)造函數(shù)
  Point(this.x, this.y);

  //重定向到主構(gòu)造函數(shù)
  Point.alongXAxis(num x) : this(x, 0);
}
五.常量構(gòu)造函數(shù)

如果你的類需要成為永遠(yuǎn)不會(huì)更改的對(duì)象,則可以使這些對(duì)象成為編譯時(shí)常量。 定義const構(gòu)造函數(shù)要確保所有實(shí)例變量都是final。

class Point {
  final num x;
  final num y;
  static final Point2 origin = const Point2(0, 0);

  //const關(guān)鍵字放在構(gòu)造函數(shù)名稱之前,且不能有函數(shù)體
  const Point2(this.x, this.y);
}
六.工廠構(gòu)造函數(shù)

不用直接創(chuàng)建對(duì)象(可以通過調(diào)用其他構(gòu)造函數(shù)創(chuàng)建)

class CommonPrivacyScreen {
  final String title;
  final String url;

  factory CommonPrivacyScreen.privacy() {
    return CommonPrivacyScreen(title: "title_privacy",url: "url_privacy");
  }

  factory CommonPrivacyScreen.agreement() {
    return CommonPrivacyScreen(title: "title_agreement",url: "title_agreement");
  }

  CommonPrivacyScreen({Key key, this.title, this.url}) : super(key: key);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容