Light's Blog

The best or nothing.

iOS知识小集-201902

| Comments

2019.02.15

Dart 语法基础:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//  Impport library: import 'dart:xxx' / 'package:xxx'
//   Specifying a library prefix: import 'package:lib2/lib2.dart' as lib2;
//   Importing only part of a library: import 'package:lib1/lib1.dart' show/hide foo;
//   Lazily loading a library: import 'package:greetings/hello.dart' deferred as hello;

import 'dart:math';

void main() {
  testVariables();
  testBuiltInTypes();
  testFunctions();
  testOperators();
  testControlFlow();
  testExceptions();
  testClasses();
  testGenerics();
  testAsynchrony();
  test();
}

//   Variables
void testVariables() {
  print("Test Variables\n");

  // Constants
  final name = "Light";
  const age = 26;
  print("name: $name  age: $age");

  // Variables
  var weather = "clear";
  weather = "cloudy";
  print("weather: $weather");

  print("\n");
}

//   Built-in types
void testBuiltInTypes() {
  print("Test Built-in types\n");

  // Numbers
  var integer = 2;
  var pi = 3.1415926;
  var one = int.parse('1');
  assert(one == 1);
  var onePointOne = double.parse('1.1');
  assert(onePointOne == 1.1);
  var twoAsString = integer.toString();
  assert(twoAsString == '2');
  var piAsString = pi.toStringAsFixed(2);
  assert(piAsString == '3.14');
  assert((3 << 1) == 6);
  assert((3 >> 1) == 1);
  assert((3 | 4) == 7);

  // Strings
  var s1 = 'hello dart !';
  var s2 = "how are you";
  print('say hello: $s1');
  print('say grate: ${s2.toUpperCase() + ' TOM ?'}');
  var s3 = '''

  multy line string
  multy line string
  ''';
  print(s3);
  var s4 = 'In a raw string, not even \n gets special treatment';
  print('not raw string: $s4');
  var s5 = r'In a raw string, not even \n gets special treatment';
  print('raw string: $s5');

  // Booleans
  var fullName = '';
  assert(fullName.isEmpty);
  var hitPoints = 0;
  assert(hitPoints <=0);
  var unicorn;
  assert(unicorn == null);
  var iMeantToDoThis = 0 / 0;
  assert(iMeantToDoThis.isNaN);

  // Lists
  var list = [1, 2, 3];
  print('list: $list');
  print('list length: ${list.length}');

  // Maps
  var personInfo = {
    'name': 'Light',
    'age': '26',
    'sex': 'male'
  };
  print('map: $personInfo');
  personInfo['birthday'] = '1992.12.09';
  print('map: $personInfo');
  print('map length: ${personInfo.length}');

  // Runes
  var clapping = '\u{1f44f}';
  print('clapping: $clapping');
  print('clapping code units: ${clapping.codeUnits}');
  print('clapping runes: ${clapping.runes.toList()}');
  Runes input = new Runes('hello \u2665  \u{1f605}  \u{1f60e}  \u{1f47b}  \u{1f596}  \u{1f44d}');
  print('runes: $input');
  print('string from runes: ${new String.fromCharCodes(input)}');

  // Symbols
  print(#radix);
  print(#bar);

  print("\n");
}

//   Functions
void testFunctions() {
  print("Test Functions\n");

  // Optional parameters
      // Optional named parameters {}
      // Optional positional parameters []
      // Default parameter values = compile time const

  // The main() function
      // a top-level function, which serves as the entrypoint to the app

  // Function as first-class objects
      // pass a function as a parameter to another function
      // assign a function to a variable

  // Anonymous functions
      // ([Type] param1[, ...]) { codeBlock}

  // Lexical scope
      // "follow the curly braces outwards" to see if a variable is in scope

  // Lexical closures
      // A closure is a function object that has access to variables in its lexical scope

  // Return values
      // All function return a value. default return value is null

  print("\n");
}

//   Operators
void testOperators() {
  print("Test Operators\n");

  print('5 / 2 = ${5 / 2}');
  print('5 ~/ 2 = ${5 ~/ 2}');
  print('5 % 2 = ${5 % 2}');
  // as is is!
  // ??=
  // ^ ~expr
  // expr1 ?? expr2
  // . ?. ..

  print("\n");
}

//   Control flow statements
void testControlFlow() {
  print("Test Control flow\n");

  // for loops
  var message = StringBuffer('Dart is fun');
  for (var i = 0; i < 5; i++) {
    message.write('!');
  }
  print(message);
  // for in
  for (var x in [1, 2, 3]) {
    print(x);
  }
  // forEach
  ['a', 'b', 'c'].forEach((char) => print(char));

  // switch
  var command = 'CLOSED';
  switch (command) {
    case 'CLOSED':
      print('Closed');
      continue open;
    open:
    case 'OPEN':
      print('Open');
      break;
    default:
      print('Default');
  }

  print("\n");
}

//   Exceptions
void testExceptions() {
  print("Test Exceptions\n");

  // throw
  // rethrow
  // try on catch finally

  print("\n");
}


class Point {
  // instantce variables
  num x, y;

  // static variables
  static var pointColor = Color.red;

  // convenient constructors
  Point(this.x, this.y);

  // named constructors with initializer list
  Point.origin()
    : x = 0,
          y = 0 {
    print('create point (0, 0)');
  }

  // redirecting constructors
  Point.alongXAxis(num x) : this(x, 0);

  // instance method
  distanceTo(Point p) => sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));

  // getter
  get distanceToOrigin => distanceTo(Point.origin());

  // static method
  static num distanceBetween(Point a, Point b) {
    var dx = a.x - b.x;
    var dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
  }
}

abstract class Doer {
  // Define instance variables and methods...

  void doSomething(); // Define an abstract method.
}

class EffectiveDoer extends Doer {
  void doSomething() {
    // Provide an implementation, so the method is not abstract here...
  }
}

// A person. The implicit interface contains greet().
class Person {
  // In the interface, but visible only in this library.
  final \_name;

  // Not in the interface, since this is a constructor.
  Person(this.\_name);

  // In the interface.
  String greet(String who) => 'Hello, $who. I am $\_name.';
}

// An implementation of the Person interface.
class Impostor implements Person {
  get \_name => '';

  String greet(String who) => 'Hi $who. Do you know who I am?';
}

String greetBob(Person person) => person.greet('Bob');

enum Color { red, green, blue }

//   Classes
void testClasses() {
  print("Test Classes\n");

  // Using constructors
  var p = Point.origin();

  // Using class members
  // Instance variables
  print('point: $p');
  print('distance between ${p.toString()} and (3, 4): ${p.distanceTo(Point(3, 4))}');

  // Getting an objects's type
  print('The type of 1 is ${1.runtimeType}');

  // Constructors
      // Constructors aren’t inherited
      // Invoking a non-default superclass constructor
      // initializer list
      // superclass's no-arg constructor
      // main class's no-arg constructor
      // Specify the superclass constructor after a colon (:), just before the constructor body.
      // Initializer list
      // Redirecting constructors
      // Factory constructors, have no access to this

  // Methods
  print('Point (5, 6) distance to origin: ${Point(5, 6).distanceToOrigin}');

  // Abstract classes
      // Abstract methods can only exist in abstract classes.
      // Abstract classes are useful for defining interfaces, often with some implementation.

  // Implicit interfaces
  print(greetBob(Person('Kathy')));
  print(greetBob(Impostor()));

  // Extends a class
      // extends super methods
      // override super methods
      // override operators
      // noSuchMethod()

  // Enumerated types
  print(Color.values);
  print(Color.red);
  print(Color.red.index);

  // Adding features to a class: mixins
      // Mixins are a way of reusing a class’s code in multiple class hierarchies

  // Class variables and methods
  print('Point color: ${Point.pointColor}');
  print('distance between (2, 6) and (3, 4): ${Point.distanceBetween(Point(2, 6), Point(3, 4))}');

  print("\n");
}

//   Generics
void testGenerics() {
  print("Test Generics\n");

  // Using collection literals
  var names = <String>['Seth', 'Kathy', 'Lars'];
  var pages = <String, String>{
    'index.html': 'Homepage',
    'robots.txt': 'Hints for web robots',
    'humans.txt': 'We are people, not machines'
  };
  print(names);
  print(pages);
  print(names.runtimeType);

  // Restricting the parameterized type
  // Using generic methods

  print("\n");
}

//   Asynchrony support
void testAsynchrony() {
  print("Test Asynchrony\n");

  // Handling Futures
  // To use await, code must be in an async function—a function marked as async

  // Declaring async functions
  // An async function is a function whose body is marked with the async modifier

  // Handling Streams
  //

  print("\n");
}



//
void test() {
  print("Test \n");

  print("\n");
}

Comments