函数

Function 类:

  1. bool isZero(int number) { //判断整数是否为0
  2. return number == 0;
  3. }
  4. void printInfo(int number,Function check) { //用check函数来判断整数是否为0
  5. print("$number is Zero: ${check(number)}");
  6. }
  7. Function f = isZero;
  8. int x = 10;
  9. int y = 0;
  10. printInfo(x,f); // 输出 10 is Zero: false
  11. printInfo(y,f); // 输出 0 is Zero: true

如果函数体只有一行, 那么可以简化:

  1. bool isZero(int number) => number == 0;
  2. void printInfo(int number,Function check) => print("$number is Zero: ${check(number)}");

Dart 认为重载会导致混乱,因此从设计之初就不支持重载,而是提供了可选命名参数可选参数

  • 可选命名参数, void func({bool a, int b}) {}
  • 可选参数, void func(bool a, [int b]) {}
  1. //要达到可选命名参数的用法,那就在定义函数的时候给参数加上 {}
  2. void enable1Flags({bool bold, bool hidden}) => print("$bold , $hidden");
  3. //定义可选命名参数时增加默认值
  4. void enable2Flags({bool bold = true, bool hidden = false}) => print("$bold ,$hidden");
  5. //可忽略的参数在函数定义时用[]符号指定
  6. void enable3Flags(bool bold, [bool hidden]) => print("$bold ,$hidden");
  7. //定义可忽略参数时增加默认值
  8. void enable4Flags(bool bold, [bool hidden = false]) => print("$bold ,$hidden");
  9. //可选命名参数函数调用
  10. enable1Flags(bold: true, hidden: false); //true, false
  11. enable1Flags(bold: true); //true, null
  12. enable2Flags(bold: false); //false, false
  13. //可忽略参数函数调用
  14. enable3Flags(true, false); //true, false
  15. enable3Flags(true,); //true, null
  16. enable4Flags(true); //true, false
  17. enable4Flags(true,true); // true, true

这里我要和你强调的是,在 Flutter 中会大量用到可选命名参数的方式,你一定要记住它的用法。
**

类的定义及初始化

顶层类: Object.

  • 实例变量与实例方法、类变量与类方法的声明与 Java 类似
  • 在声明变量与方法时,在前面加上“”即可作为 private 方法使用。如果不加“”,则默认为 public。不过,“_”的限制范围并不是类访问级别的,而是库访问级别。
  1. class Point {
  2. num x, y;
  3. static num factor = 0;
  4. //语法糖,等同于在函数体内:this.x = x;this.y = y;
  5. Point(this.x,this.y);
  6. void printInfo() => print('($x, $y)');
  7. static void printZValue() => print('$factor');
  8. }
  9. var p = new Point(100,200); // new 关键字可以省略
  10. p.printInfo(); // 输出(100, 200);
  11. Point.factor = 10;
  12. Point.printZValue(); // 输出10
  • Dart 还提供了命名构造函数的方式,使得类的实例化过程语义更清晰。
  • Dart 支持初始化列表。在构造函数的函数体真正执行之前,你还有机会给实例变量赋值,甚至重定向至另一个构造函数。
  1. class Point {
  2. num x, y, z;
  3. Point(this.x, this.y) : z = 0; // 初始化变量z
  4. // 应该是命名构造函数
  5. Point.bottom(num x) : this(x, 0); // 重定向构造函数
  6. void printInfo() => print('($x,$y,$z)');
  7. }
  8. var p = Point.bottom(100);
  9. p.printInfo(); // 输出(100,0,0)

复用

继承父类和接口实现:

  • 继承父类意味着,子类由父类派生,会自动获取父类的成员变量和方法实现,子类可以根据需要覆写构造函数及父类方法;
  • 接口实现则意味着,子类获取到的仅仅是接口的成员变量符号和方法符号,需要重新实现成员变量,以及方法的声明和初始化,否则编译器会报错。

接下来,我以一个例子和你说明在 Dart 中继承和接口的差别:

Vector 通过继承 Point 的方式增加了成员变量,并覆写了 printInfo 的实现;而 Coordinate,则通过接口实现的方式,覆写了 Point 的变量定义及函数实现:

  1. class Point {
  2. num x = 0, y = 0;
  3. void printInfo() => print('($x,$y)');
  4. }
  5. //Vector继承自Point
  6. class Vector extends Point{
  7. num z = 0;
  8. @override
  9. void printInfo() => print('($x,$y,$z)'); //覆写了printInfo实现
  10. }
  11. //Coordinate是对Point的接口实现
  12. class Coordinate implements Point {
  13. num x = 0, y = 0; //成员变量需要重新声明
  14. void printInfo() => print('($x,$y)'); //成员函数需要重新声明实现
  15. }
  16. var xxx = Vector();
  17. xxx
  18. ..x = 1
  19. ..y = 2
  20. ..z = 3; //级联运算符,等同于xxx.x=1; xxx.y=2;xxx.z=3;
  21. xxx.printInfo(); //输出(1,2,3)
  22. var yyy = Coordinate();
  23. yyy
  24. ..x = 1
  25. ..y = 2; //级联运算符,等同于yyy.x=1; yyy.y=2;
  26. yyy.printInfo(); //输出(1,2)
  27. print (yyy is Point); //true
  28. print(yyy is Coordinate); //true
  • Dart 还提供了另一种机制来实现类的复用,即“混入”(Mixin) (多重继承)

备注:继承歧义,也叫菱形问题,是支持多继承的编程语言中一个相当棘手的问题。当 B 类和 C 类继承自 A 类,而 D 类继承自 B 类和 C 类时会产生歧义。如果 A 中有一个方法在 B 和 C 中已经覆写,而 D 没有覆写它,那么 D 继承的方法的版本是 B 类,还是 C 类的呢?

要使用混入,只需要 with 关键字即可。我们来试着改造 Coordinate 的实现,把类中的变量声明和函数实现全部删掉:

  1. class Coordinate with Point {
  2. }
  3. var yyy = Coordinate();
  4. print (yyy is Point); //true
  5. print(yyy is Coordinate); //true

通过混入,一个类里可以以非继承的方式使用其他类中的变量与方法,效果正如你想象的那样。

运算符

Dart 多了几个额外的运算符,用于简化处理变量实例缺失(即 null)的情况。

  • ?. 运算符:假设 Point 类有 printInfo() 方法,p 是 Point 的一个可能为 null 的实例。那么,p 调用成员方法的安全代码,可以简化为 p?.printInfo() ,表示 p 为 null 的时候跳过,避免抛出异常。
  • ??= 运算符:如果 a 为 null,则给 a 赋值 value,否则跳过。这种用默认值兜底的赋值语句在 Dart 中我们可以用 a ??= value 表示。
  • ?? 运算符:如果 a 不为 null,返回 a 的值,否则返回 b。在 Java 或者 C++ 中,我们需要通过三元表达式 (a != null)? a : b 来实现这种情况。而在 Dart 中,这类代码可以简化为 a ?? b

在 Dart 中,一切都是对象,就连运算符也是对象成员函数的一部分

对于系统的运算符,一般情况下只支持基本数据类型和标准库中提供的类型。而对于用户自定义的类,如果想支持基本操作,比如比较大小、相加相减等,则需要用户自己来定义关于这个运算符的具体实现。

运算符重载?

  1. class Vector {
  2. num x, y;
  3. Vector(this.x, this.y);
  4. // 自定义相加运算符,实现向量相加
  5. Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
  6. // 覆写相等运算符,判断向量相等
  7. bool operator == (dynamic v) => x == v.x && y == v.y;
  8. }
  9. final x = Vector(3, 3);
  10. final y = Vector(2, 2);
  11. final z = Vector(1, 1);
  12. print(x == (y + z)); // 输出true

operator 是 Dart 的关键字,与运算符一起使用,表示一个类成员运算符函数。在理解时,我们应该把 operator 和运算符作为整体,看作是一个成员函数名。