|
| 1 | +import 'dart:ffi'; |
| 2 | +import 'dart:io'; |
| 3 | + |
1 | 4 | void main() {
|
2 | 5 | print('Hello World');
|
3 | 6 | }
|
| 7 | + |
| 8 | +var name = "Tom"; |
| 9 | +var year = 2024; |
| 10 | +var temp = 31.3; |
| 11 | +var languages = ["C", "C#", "Go", "Dart"]; |
| 12 | +var person = { |
| 13 | + 'name': "Lee", |
| 14 | + 'age': 18, |
| 15 | + 'hobby': ['eat', 'sleep'] |
| 16 | +}; |
| 17 | + |
| 18 | +bool odd(int number) { |
| 19 | + if (number % 2 == 0) { |
| 20 | + return false; |
| 21 | + } else { |
| 22 | + return true; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +void loop(int count) { |
| 27 | + for (int i = 0; i < count; i++) { |
| 28 | + print("i"); |
| 29 | + } |
| 30 | + |
| 31 | + var hobbies = ["eat", "sleep", "play"]; |
| 32 | + for (var hobby in hobbies) { |
| 33 | + print(hobby); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +int fib(int n) { |
| 38 | + if (n == 0 || n == 1) { |
| 39 | + return n; |
| 40 | + } |
| 41 | + return fib(n - 1) + fib(n - 2); |
| 42 | +} |
| 43 | + |
| 44 | +var result = fib(10); |
| 45 | + |
| 46 | +void func() { |
| 47 | + var hobbies = ["eat", "sleep", "play"]; |
| 48 | + hobbies.where((hobby) => hobby.endsWith("t")).forEach(print); |
| 49 | +} |
| 50 | + |
| 51 | +class Spacecraft { |
| 52 | + String name; |
| 53 | + DateTime? time; |
| 54 | + |
| 55 | + //这是一个getter方法 |
| 56 | + int? get timeYear => time?.year; |
| 57 | + |
| 58 | + //构造函数 |
| 59 | + Spacecraft(this.name, this.time) {} |
| 60 | + |
| 61 | + //这也是构造函数 |
| 62 | + Spacecraft.noTime(String name) : this(name, null); |
| 63 | + |
| 64 | + void describe() { |
| 65 | + //这是我见过最简单的模板字符串 既没有要求是特殊的引号封装,也没有要求${}写法 |
| 66 | + print("Spacecraft:$name"); |
| 67 | + var time = this.time; |
| 68 | + if (time != null) { |
| 69 | + int years = DateTime.now().difference(time).inDays ~/ 365; |
| 70 | + print('Launched: $time ($years years ago)'); |
| 71 | + } else { |
| 72 | + print("no time"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +var voyager = Spacecraft('Voyager', DateTime(2000, 1, 1)); |
| 78 | + |
| 79 | +var voyager2 = Spacecraft.noTime('Voyager2'); |
| 80 | + |
| 81 | +//枚举 |
| 82 | +enum Status { INIT, SUCCESS, FAIL, DEATH } |
| 83 | + |
| 84 | +//增强枚举 |
| 85 | +enum Os { |
| 86 | + Win(name: "Windows", year: 1998), |
| 87 | + Mac(name: "Mac", year: 1997), |
| 88 | + Linux(name: "Linux", year: 2002); |
| 89 | + |
| 90 | + final String name; |
| 91 | + final int year; |
| 92 | + |
| 93 | + const Os({required this.name, required this.year}); |
| 94 | + |
| 95 | + bool get isWin => this == Os.Win; |
| 96 | +} |
| 97 | + |
| 98 | +var win10 = Os.Win; |
| 99 | + |
| 100 | +//扩展类 |
| 101 | +class Orbiter extends Spacecraft { |
| 102 | + double altitude; |
| 103 | + Orbiter(super.name, super.time, this.altitude); |
| 104 | + |
| 105 | + @override |
| 106 | + void describe() { |
| 107 | + print("this is Orbiter and altitude is $altitude"); |
| 108 | + super.describe(); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +mixin Piloted { |
| 113 | + int astronauts = 1; |
| 114 | + |
| 115 | + void describeCrew() { |
| 116 | + print('Number of astronauts: $astronauts'); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +//mixin-with 可以让一个类获得增强 但mixin中的成员不属于this |
| 121 | +class PilotedCraft extends Spacecraft with Piloted { |
| 122 | + PilotedCraft(super.name, super.time); |
| 123 | + |
| 124 | + void say() { |
| 125 | + print(astronauts); |
| 126 | + describeCrew(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +//所有的类都隐式定义成了一个接口。因此,任意类都可以作为接口被实现 |
| 131 | +class MockSpaceship implements Spacecraft { |
| 132 | + @override |
| 133 | + String name; |
| 134 | + |
| 135 | + @override |
| 136 | + DateTime? time; |
| 137 | + |
| 138 | + @override |
| 139 | + void describe() { |
| 140 | + // TODO: implement describe |
| 141 | + } |
| 142 | + |
| 143 | + @override |
| 144 | + // TODO: implement timeYear |
| 145 | + int? get timeYear => throw UnimplementedError(); |
| 146 | + |
| 147 | + MockSpaceship(this.name); |
| 148 | +} |
| 149 | + |
| 150 | +abstract class Person { |
| 151 | + void sayHello(); |
| 152 | + void sayBye() { |
| 153 | + print("bye"); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +//async await |
| 158 | +const oneSecond = Duration(seconds: 1); |
| 159 | + |
| 160 | +Future<void> printWithDelay(String message) { |
| 161 | + return Future.delayed(oneSecond).then((_) { |
| 162 | + print(message); |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +Future<void> printWithDelayAwait(String message) async { |
| 167 | + await Future.delayed(oneSecond); |
| 168 | + print("message"); |
| 169 | +} |
| 170 | + |
| 171 | +Future<void> createDescriptions(Iterable<String> fileNames) async { |
| 172 | + for (final fileName in fileNames) { |
| 173 | + try { |
| 174 | + var file = File(fileName); |
| 175 | + if (await file.exists()) { |
| 176 | + var modified = await file.lastModified(); |
| 177 | + print( |
| 178 | + 'File for $fileName already exists. It was modified on $modified'); |
| 179 | + } else { |
| 180 | + await file.create(); |
| 181 | + await file.writeAsString("create File $fileName"); |
| 182 | + } |
| 183 | + } on IOException catch (e) { |
| 184 | + print("create file $fileName exception:$e"); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments