// import 'package:operation/operation.dart' as operation;
main(List<String> arguments) {
toInt();
Person p = Person();
// 级联
p..name = 'wang'
..country = 'USA'
..setCountry('CHINA');
print(p);
// if 语句
int i = -2;
if (i < 0) {
print('i < 0');
}else if (i == 0) {
print('i == 0');
}else {
print('i > 0');
}
// 循环
for (int i = 0; i < 3; i++) {
print(i);
}
print('------------------------');
var collection = [1, 2, 3];
collection.forEach((x) => print(x)); // forEach的参数为Function
print('------------------------');
for(var x in collection) {
print(x);
}
// switch的参数可以是num, 或者String
var command = 'OPEN';
switch (command) {
case 'OPEN':
print('OPEN');
break;
case 'CLOSE':
print('CLOSE');
break;
default:
print('DEFAULT');
}
// 异常处理
try {
throw 'this is a Exception';
} on Exception catch (e) {
print('unknown exception: $e');
} catch (e) {
print('unknown type: $e');
}finally {
print('close');
}
}
toInt() {
int a = 3;
int b = 2;
// 取整
print(a~/b);
}
class Person {
String name;
String country;
void setCountry(String country) {
this.country = country;
}
String toString() => 'Name: $name\nCountry: $country';
}