x == y; x .== y; //==是定义以在左侧对象的函数 b ??= value; //如果 b 是 null,则赋值给 b;如果不是 null,则 b 的值保持不变
级联操作符:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
querySelector('#button') // Get an object. ..text = 'Confirm'// Use its members. ..classes.add('important') ..onClick.listen((e) => window.alert('Confirmed!')); //级联调用可以嵌套 final addressBook = (new AddressBookBuilder() ..name = 'jenny' ..email = 'jenny@example.com' ..phone = (new PhoneNumberBuilder() ..number = '415-555-0100' ..label = 'home') .build()) .build(); //无法再void上使用级联操作
Control flow statements
for、forEach(与c++相同)
switch中可用continue跳转至一个标签
1 2 3 4 5 6 7 8 9 10 11
var command = 'CLOSED'; switch (command) { case'CLOSED': executeClosed(); continue nowClosed; // Continues executing at the nowClosed label. nowClosed: case'NOW_CLOSED': // Runs for both CLOSED and NOW_CLOSED. executeNowClosed(); break; }
Class
Constructor(构造函数)
一般构造函数与语法糖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classPoint{ num x, y;
Point(num x, num y){ this.x = x; this.y = y; } }
//使用语法糖简化构造函数 classPoint{ num x, y;
// Syntactic sugar for setting x and y // before the constructor body runs. Point(this.x, this.y); }
命名构造函数
1 2 3 4 5 6 7 8 9 10 11 12
classPoint{ num x; num y;
Point(this.x, this.y);
// Named constructor Point.fromJson(Map json) { x = json['x']; y = json['y']; } }
classEmployeeextendsPerson{ // Person does not have a default constructor; // you must call super.fromJson(data). Employee.fromJson(Map data) : super.fromJson(data) { print('in Employee'); } }
main() { var emp = new Employee.fromJson({});
// Prints: // in Person // in Employee if (emp is Person) { // Type check emp.firstName = 'Bob'; } (emp as Person).firstName = 'Bob'; }
初始化列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//通过在参数列表后加':',通过逗号分隔表达式 classPoint{ num x; num y;
Point(this.x, this.y);
// Initializer list sets instance variables before // the constructor body runs. Point.fromJson(Map jsonMap) : x = jsonMap['x'], y = jsonMap['y'] { print('In Point.fromJson(): ($x, $y)'); } }
重定向构造函数
1 2 3 4 5 6 7 8 9 10 11 12
//有时候一个构造函数会调动类中的其他构造函数。 一个重定向构造函数是没有代码的,在构造函数声明后,使用 冒号调用其他构造函数。 classPoint{ num x; num y;
// The main constructor for this class. Point(this.x, this.y);
// Delegates to the main constructor. //调用完此构造函数后再调用主构造函数 Point.alongXAxis(num x) : this(x, 0); }