天天看點

Flutter系列之Dart函數、類與運算符

程式設計語言雖然千差萬别,但歸根結底,設計思想無非是表示資訊與處理資訊

在Flutter系列之Dart語言概述中已經介紹了Dart如何表示資訊,本篇将介紹Dart是如何處理資訊的

作為一門真正面向對象的程式設計語言,Dart将處理資訊的過程抽象為了對象,而函數、類與運算符則是抽象中最重要的手段

函數

函數是一段用來獨立完成某個功能的代碼片段,而Dart中所有類型都是對象類型,函數也不例外,即函數也是對象,它的類型為Function。

void main() {
  Function check = isEmptyStr;
  printStr('',check);
  printStr('Hello World!',check);
}

bool isEmptyStr(String str){//判斷字元串是否為null
  return str.isEmpty;
}

void printStr(String str,Function check){//用check函數來判斷String是否為null
  if (check(str)) {
    print('$str is null');
  }else {
    print('$str is not null');
  }
}
           

結果如下

is null
Hello World! is not null
           

上面代碼中,首先定義了一個判斷字元串是否為null的函數isEmptyStr,并把它傳給了另一個列印輸出的函數printStr,即函數也可以被定義為變量,甚至可以作為參數傳遞給其他函數

日常開發中,經常會遇到一個函數中需要傳遞多個參數的情況,這種情況下Dart與其他語言如c、c++、java等的做法是不同的

c、c++、java等的做法是提供函數的重載,即函數同名但參數不同,而Dart認為重載會導緻混亂,是以不支援重載,而是提供了可選參數和可選命名參數

具體方式為,在申明函數時:

·給參數添加{},以paramName:value的方式指定調用參數,即可選命名參數

·給參數加[],則意味着這些參數可以忽略,即可選參數

不僅如此,在使用這兩種方式定義函數時,還可以給參數設定預設值

注意:可選參數和可選命名參數不可一起使用,可選參數和可選命名參數在申明時需放在其他參數後面,且可選命名參數調用時與申明的順序無關,但可選參數調用時與申明的順序有關

void main() {
  _ptionalNamedParameter('Tom',2,sex:'男');
  _ptionalNamedParameter('Tom',2,sex:'男',age:5);
  _ptionalNamedParameter('Tom',2);
  _ptionalNamedParameter('Tom',2,age:25);
  _ptionalNamedParameter('Tom',2,age:18,sex:'女');

  _ptionalParameter('Tom',2,'男');
  _ptionalParameter('Tom',2,'男',5);
  _ptionalParameter('Tom',2);
  //_ptionalParameter('Tom',2,25);//錯誤
  //_ptionalParameter('Tom',2,18,'女');//錯誤
}

bool isNotEmptyStr(String str) {
  return str.isNotEmpty;
}

void printStr(String str,Function check){//用check函數來判斷String是否為null
  if (check(str)) {
    print('$str is null');
  }else {
    print('$str is not null');
  }
}

//可選命名參數:在需要定義為可選命名參數時給參數加{},并且可以給可選命名參數設定預設值,必須定義在其他參數後面,參數順序可變,不可與可選參數同用
void _ptionalNamedParameter(String name, int classes, {String sex, int age = 18}) {
  print('_ptionalNamedParameter:name is $name,classes = $classes,sex is $sex,age = $age');
}

//可選參數:在需要定義為可選參數時給參數加[],并且可以給可選參數設定預設值,必須定義在其他參數後面,參數順序不可變,不可與可選命名參數同用
void _ptionalParameter(String name, int classes, [String sex = '男', int age]) {
  print('_ptionalParameter:name is $name,classes = $classes,sex is $sex,age = $age');
}
           

結果如下

_ptionalNamedParameter:name is Tom,classes = 2,sex is 男,age = 18
_ptionalNamedParameter:name is Tom,classes = 2,sex is 男,age = 5
_ptionalNamedParameter:name is Tom,classes = 2,sex is null,age = 18
_ptionalNamedParameter:name is Tom,classes = 2,sex is null,age = 25
_ptionalNamedParameter:name is Tom,classes = 2,sex is 女,age = 18
_ptionalParameter:name is Tom,classes = 2,sex is 男,age = null
_ptionalParameter:name is Tom,classes = 2,sex is 男,age = 5
_ptionalParameter:name is Tom,classes = 2,sex is 男,age = null
           

類是特定類型的資料和方法的集合,也是建立對象的模闆

類的定義及初始化

Dart是面向對象的語言,每個對象都是一個類的執行個體,都繼承自頂層類型Object

注意:Dart中并沒有public、protected、private等關鍵字,取而代之的是隻要在申明變量和方法時,在其前面加上“”即可作為private使用,如果不加“”,則預設為public。不過,“_”的限制範圍并不是類通路級别的,而是庫(package)通路級别的

void main() {
	Student student = new Student('Jack', '男', age:18);
  student.printStudentInfo();
  var s=Student('Tom', '男');//省略new關鍵字
  s.printStudentInfo();
  Student.studentId = 1;
  Student.printStudentId();
}

class Student {
  String name;
  String sex;
  int age;
  static int studentId = 0;

  //文法糖,相當于在函數體内:this.name=name;this.sex=sex;this.age=age;
  Student(this.name, this.sex, {this.age});

  void printStudentInfo() => print('name is $name, sex is $sex, age is $age');

  static void printStudentId() => print('studentId is $studentId');
}
           

結果如下

name is Jack, sex is 男, age is 18
name is Tom, sex is 男, age is null
studentId is 1
           

上面示例中,定義了三個成員變量,并通過構造函數文法糖對三個成員變量進行初始化操作;定義了一個靜态成員變量,并在初始化時指派預設值,并定義了兩個方法分别列印三個成員變量及一個靜态成員變量的值

有的時候類的執行個體化需要根據參數提供多種初始化方式。除了可選命名參數和可選參數之外,Dart還提供了命名構造函數的方式,使得執行個體化過程語義更清晰

注意:Dart支援初始化清單。在構造函數真正執行之前,可以先給執行個體變量指派,甚至可以重定向之另一個構造函數

void main() {
  Student student = new Student.nameInfo('Tom');
  student.printStudentInfo();
}

class Student {
  String name;
  String sex;
  int age;

  Student(this.name, this.sex) : age = 18; //初始化變量age

  Student.nameInfo(String name) : this(name, '男'); //重定向構造函數

  void printStudentInfo() => print('name is $name, sex is $sex, age is $age');
}
           

結果如下

name is Tom, sex is 男, age is 18
           

上面示例中,Student中有兩個構造函數:Student和Studnet.nameInfo,其中,Studnet.nameInfo将成員變量的初始化重定向到了Student中,而Student則在初始化清單中為age指派預設值

複用

在面向對象的程式設計語言中,将其他類的變量與方法納入本類中進行複用的方式一般有兩種:繼承和接口,而Dart也是面向對象的程式設計語言,是以,在Dart中,複用的方式也是繼承和接口

繼承:子類由父類派生,會自動擷取父類的成員變量和方法實作,子類可以根據需要覆寫構造函數及父類方法

接口:子類擷取到的僅僅是接口的成員變量符号和方法符号,需要重新實作成員變量,以及方法的申明和初始化,否則編譯器會報錯

以下示例會對繼承和接口的差別進行說明

void main() {
Android android = new Android();
  android
    ..brandName = '華為'
    ..phoneModel = 'P30 Pro'
    ..edition = 'EMUI9.1.1';//級聯運算符,相當于android.brandName = '華為';android.phoneModel = 'P30 Pro';android.edition = 'EMUI9.1.1';
  android.printInfo();

  IOS ios = new IOS();
  ios
    ..phoneModel = 'iPhone XR'
    ..edition = 'iOS 12';//級聯運算符,相當于phone.phoneModel = 'iPhone XR';phone.edition = 'iOS 12';
  ios.printInfo();
}

class Phone {
  String phoneModel; //手機型号
  String edition; //手機版本

  void printInfo() =>
      print('This Phone phoneModel is $phoneModel, and edition is $edition');
}

//Android繼承自Phone
class Android extends Phone {
  String brandName; //品牌
  @override
  void printInfo() {
    //覆寫了printInfo實作
    print('This Phone brandName is $brandName, phoneModel is $phoneModel, and edition is $edition');
  }
}

//IOS實作了Phone接口
class IOS implements Phone {
  //成員變量需要重新申明
  String phoneModel;
  String edition;
  //方法需要重新實作及初始化
  void printInfo() => print('This is IOS Phone, phoneModel is $phoneModel, and edition is $edition');
}
           

結果如下

This Phone brandName is 華為, phoneModel is P30 Pro, and edition is EMUI9.1.1
This is IOS Phone, phoneModel is iPhone XR, and edition is iOS 12
           

以上代碼為Android通過繼承Phone的方式添加了成員變量,并覆寫了printInfo()的實作;IOS通過接口實作的方式,覆寫了Phone的變量定義及方法實作;而從以上代碼也能看出,接口并沒有為我們帶來實際好處

注意:Dart和其他程式設計語言一樣,也不支援多繼承

其實,Dart除了繼承和接口外,還給我們提供了另一種機制來實作類的複用,即“混入”(Mixin):混入鼓勵代碼重用,可以認為是具有實作方法的接口

要使用混入,隻需要with關鍵字即可

void main() {
  IOS ios = new IOS();
  ios
    ..phoneModel = 'iPhone XR'
    ..edition = 'iOS 12';//級聯運算符,相當于phone.phoneModel = 'iPhone XR';phone.edition = 'iOS 12';
  ios.printInfo();
}

class Phone {
  String phoneModel; //手機型号
  String edition; //手機版本

  void printInfo() =>
      print('This Phone phoneModel is $phoneModel, and edition is $edition');
}

class IOS with Phone {}
           

結果如下

This Phone phoneModel is iPhone XR, and edition is iOS 12
           

如上示例可知:通過混入,一個類裡可以以非繼承的方式使用其他類中的變量和方法,即通過混入,可以解決單繼承問題

運算符

Dart除了和其他大部分程式設計語言的運算符一樣外,還提供了幾個額外的運算符,用于簡化處理變量執行個體缺失(null)的情況

?.運算符:假設Phone類中有個printInfo()方法,phone是Phone類的一個可能為null的執行個體,phone調用成員方法的安全方法,可以簡化為phone.?printInfo(),表示如果phone為null,則跳過printInfo()方法

??=運算符:如果變量a為null,則給a指派value,負責跳過。a??=value

??運算符:如果a不為null,傳回a的值,否則傳回b的值。a??b

void main() {
  Phone phone;
  phone?.printInfo();//由于phone為null,是以不會執行printInfo()方法
  Test();
}

class Phone {
  String phoneModel; //手機型号
  String edition; //手機版本

  void printInfo() =>
      print('This Phone phoneModel is $phoneModel, and edition is $edition');
}

void Test(){
  int age;
  print('age=$age');
  print('age=${age??16}');//??運算符,age為null,傳回16
  age??=18;//??=運算符,age為null,給age指派為18
  print('age=$age');
  print('age=${age??20}');//??運算符,age不為null,傳回18
  age??=22;//??=運算符,age不為null,傳回18
  print('age=$age');
}
           

結果如下

age=null
age=16
age=18
age=18
age=18
           

在Dart中,一切都是對象,連運算符也是對象成員方法的一部分

對于系統的運算符,一般情況下隻支援基本資料類型和标準庫中提供的類型。而對于使用者自定義的類,如果想支援基本操作,比如比較大小、相加相減等,則需要使用者自己來定義關于這個運算符的具體實作

Dart提供了運算符的覆寫機制,我們不僅可以覆寫方法,還可以覆寫或者自定義運算符

operator是Dart的關鍵字,與運算符一起使用,表示一個類成員運算符方法

void main() {
  Vector v = new Vector(5, 3);
  Vector v2 = new Vector(1, 2);
  Vector v3 = new Vector(4, 1);
  Vector v4 = v - v2;
  print('x = ${v4.x}, y = ${v4.y}');
  print(v3 == (v - v2));
}

class Vector {
  num x, y;

  Vector(this.x, this.y);

  //自定義相減運算符
  Vector operator -(Vector v) {
    return Vector(x - v.x, y - v.y);
  }

  //覆寫相等運算符
  bool operator ==(dynamic v) {
    return (x == v.x && y == v.y);
  }
}
           

結果如下

x = 4, y = 1
true
           

文章已同步更新至微信公衆号,歡迎關注

Flutter系列之Dart函數、類與運算符

繼續閱讀