标记特殊需要记忆的点

1. 取整~/操作符之前可能很少看到,代码如下

1
2
3
int a = 3
int b = 2
print(a~/b);//输出1

2. 级联操作符,当你要对一个单一的对象进行一系列的操作的时候,

可以使用级联操作符 ..(相当于 js 的链式调用,隐式的返回原对象)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Person {
String name;
String country;
void setCountry(String country){
this.country = country;
}
String toString() => 'Name:$name\nCountry:$country';
}
void main() {
Person p = new Person();
p ..name = 'Wang'
..setCountry('China');
print(p);
}

3. If语句的判断条件为==bool==值,用法和大多语言一样(只接受布尔值, 其他类型的值都译为false

1
2
3
4
5
6
7
if(i<0){
print('i < 0');
}else if(i ==0){
print('i = 0');
} else {
print('i > 0');
}

4. 循环 for forEach for-in

1
2
3
for(int i = 0; i<3; i++) {
print(i);
}

如果迭代的对象是容器,那么可以使用forEach或者for-in

1
2
3
4
5
6
7
8
var collection = [0, 1, 2];

// forEach的参数为Function
collection.forEach(print);

for(var x in collection) {
print(x);
}

另外,whiledo-while没有什么变化

1
2
3
4
5
6
7
while(boolean){
//do something
}

do{
//do something
} while(boolean)

5. swith的参数可以是num,或者String

1
2
3
4
5
6
7
8
9
var command = 'OPEN';
switch (command) {
case 'CLOSED':
break;
case 'OPEN':
break;
default:
print('Default');
}

特殊用法,使用==continue== & flag ,其他和 js 没什么区别

1
2
3
4
5
6
7
8
9
10
11
12
13
var command = 'CLOSED';
switch (command) {
case 'CLOSED':
print('CLOSED');
continue nowClosed; // Continues executing at the nowClosed flag.
case 'OPEN':
print('OPEN');
break;
nowClosed: // Runs for both CLOSED and NOW_CLOSED.
case 'NOW_CLOSED':
print('NOW_CLOSED');
break;
}