1. // import 'package:operation/operation.dart' as operation;
    2. main(List<String> arguments) {
    3. toInt();
    4. Person p = Person();
    5. // 级联
    6. p..name = 'wang'
    7. ..country = 'USA'
    8. ..setCountry('CHINA');
    9. print(p);
    10. // if 语句
    11. int i = -2;
    12. if (i < 0) {
    13. print('i < 0');
    14. }else if (i == 0) {
    15. print('i == 0');
    16. }else {
    17. print('i > 0');
    18. }
    19. // 循环
    20. for (int i = 0; i < 3; i++) {
    21. print(i);
    22. }
    23. print('------------------------');
    24. var collection = [1, 2, 3];
    25. collection.forEach((x) => print(x)); // forEach的参数为Function
    26. print('------------------------');
    27. for(var x in collection) {
    28. print(x);
    29. }
    30. // switch的参数可以是num, 或者String
    31. var command = 'OPEN';
    32. switch (command) {
    33. case 'OPEN':
    34. print('OPEN');
    35. break;
    36. case 'CLOSE':
    37. print('CLOSE');
    38. break;
    39. default:
    40. print('DEFAULT');
    41. }
    42. // 异常处理
    43. try {
    44. throw 'this is a Exception';
    45. } on Exception catch (e) {
    46. print('unknown exception: $e');
    47. } catch (e) {
    48. print('unknown type: $e');
    49. }finally {
    50. print('close');
    51. }
    52. }
    53. toInt() {
    54. int a = 3;
    55. int b = 2;
    56. // 取整
    57. print(a~/b);
    58. }
    59. class Person {
    60. String name;
    61. String country;
    62. void setCountry(String country) {
    63. this.country = country;
    64. }
    65. String toString() => 'Name: $name\nCountry: $country';
    66. }