| OLD | NEW |
| 1 Dart is an object-oriented language with classes | 1 Dart is an object-oriented language with classes |
| 2 and single inheritance. The root class is | 2 and single inheritance. The root class is |
| 3 [Object](http://api.dartlang.org/dart_core/Object.html). | 3 [Object](http://api.dartlang.org/dart_core/Object.html). |
| 4 | 4 |
| 5 ### Instance variables | 5 ### Instance variables |
| 6 | 6 |
| 7 Here's how you declare | 7 Here's how you declare |
| 8 a class with instance variables | 8 a class with instance variables |
| 9 (also known as member variables): | 9 (also known as member variables): |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 {% endhighlight %} | 35 {% endhighlight %} |
| 36 | 36 |
| 37 #### Instance variable initialization | 37 #### Instance variable initialization |
| 38 | 38 |
| 39 If you initialize an instance variable | 39 If you initialize an instance variable |
| 40 where it is declared | 40 where it is declared |
| 41 (instead of in a constructor or method), | 41 (instead of in a constructor or method), |
| 42 the initial value must be a compile-time constant. | 42 the initial value must be a compile-time constant. |
| 43 | 43 |
| 44 <aside class="note" markdown="1"> | 44 <aside> |
| 45 **Note:** This restriction is currently under review. | 45 <div class="alert alert-info"> |
| 46 <strong>Note:</strong> |
| 47 This restriction is currently under review. |
| 48 </div> |
| 46 </aside> | 49 </aside> |
| 47 | 50 |
| 48 An example of a compile-time constant is a number literal: | 51 An example of a compile-time constant is a number literal: |
| 49 | 52 |
| 50 {% highlight dart %} | 53 {% highlight dart %} |
| 51 class Point { | 54 class Point { |
| 52 num x = 0, | 55 num x = 0, |
| 53 y = 0; | 56 y = 0; |
| 54 } | 57 } |
| 55 {% endhighlight %} | 58 {% endhighlight %} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 71 Point(num x, num y) { | 74 Point(num x, num y) { |
| 72 // There's a better way to do this, stay tuned. | 75 // There's a better way to do this, stay tuned. |
| 73 this.x = x; | 76 this.x = x; |
| 74 this.y = y; | 77 this.y = y; |
| 75 } | 78 } |
| 76 } | 79 } |
| 77 {% endhighlight %} | 80 {% endhighlight %} |
| 78 | 81 |
| 79 The `this` keyword references the current instance. | 82 The `this` keyword references the current instance. |
| 80 | 83 |
| 81 <aside class="note" markdown="1"> | 84 <aside> |
| 82 **Note:** Use `this` only when there is a name | 85 <div class="alert alert-info"> |
| 83 conflict. Otherwise, Dart style omits the `this`. | 86 <strong>Tip:</strong> |
| 87 Use <code>this</code> only when there is a name |
| 88 conflict. Otherwise, Dart style omits the <code>this</code>. |
| 89 </div> |
| 84 </aside> | 90 </aside> |
| 85 | 91 |
| 86 The pattern of assigning a constructor argument to | 92 The pattern of assigning a constructor argument to |
| 87 a member variable is so common, Dart has syntactic sugar | 93 a member variable is so common, Dart has syntactic sugar |
| 88 to make it easy. | 94 to make it easy. |
| 89 | 95 |
| 90 {% highlight dart %} | 96 {% highlight dart %} |
| 91 class Point { | 97 class Point { |
| 92 num x, y; | 98 num x, y; |
| 93 | 99 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 232 |
| 227 {% highlight dart %} | 233 {% highlight dart %} |
| 228 void main() { | 234 void main() { |
| 229 var a = const Point(1, 1); | 235 var a = const Point(1, 1); |
| 230 var b = const Point(1, 1); | 236 var b = const Point(1, 1); |
| 231 | 237 |
| 232 assert(a === b); // They are the same instance! | 238 assert(a === b); // They are the same instance! |
| 233 } | 239 } |
| 234 {% endhighlight %} | 240 {% endhighlight %} |
| 235 | 241 |
| 236 <aside class="note" markdown="1"> | 242 <aside> |
| 237 **Note:** Other examples of compile-time | 243 <div class="alert alert-info"> |
| 238 constants are literal numbers and literal strings. | 244 <strong>Tip:</strong> |
| 245 Other examples of compile-time |
| 246 constants are literal numbers and literal strings. |
| 247 </div> |
| 239 </aside> | 248 </aside> |
| 240 | 249 |
| 241 #### Factory constructors | 250 #### Factory constructors |
| 242 | 251 |
| 243 Use the `factory` keyword | 252 Use the `factory` keyword |
| 244 when implementing a constructor that | 253 when implementing a constructor that |
| 245 doesn't always create a new instance of its class. | 254 doesn't always create a new instance of its class. |
| 246 For example, a factory constructor | 255 For example, a factory constructor |
| 247 might return an instance from a cache, | 256 might return an instance from a cache, |
| 248 or it might return an instance of a subclass. | 257 or it might return an instance of a subclass. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 {% endhighlight %} | 291 {% endhighlight %} |
| 283 | 292 |
| 284 As for other constructors, | 293 As for other constructors, |
| 285 to invoke a factory constructor you use the `new` keyword: | 294 to invoke a factory constructor you use the `new` keyword: |
| 286 | 295 |
| 287 {% highlight dart %} | 296 {% highlight dart %} |
| 288 var logger = new Logger('UI'); | 297 var logger = new Logger('UI'); |
| 289 logger.log('Button clicked'); | 298 logger.log('Button clicked'); |
| 290 {% endhighlight %} | 299 {% endhighlight %} |
| 291 | 300 |
| 292 <aside class="note" markdown="1"> | 301 <aside> |
| 293 **Note:** Factory constructors have no access to `this`. | 302 <div class="alert alert-info"> |
| 303 <strong>Tip:</strong> |
| 304 Factory constructors have no access to <code>this</code>. |
| 305 </div> |
| 294 </aside> | 306 </aside> |
| 295 | 307 |
| 296 ### Methods | 308 ### Methods |
| 297 | 309 |
| 298 Methods are functions that | 310 Methods are functions that |
| 299 provide behavior for an object. | 311 provide behavior for an object. |
| 300 | 312 |
| 301 #### Instance methods | 313 #### Instance methods |
| 302 | 314 |
| 303 Instance methods on objects can access instance variables and `this`. | 315 Instance methods on objects can access instance variables and `this`. |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 rect.right = 12; | 368 rect.right = 12; |
| 357 assert(rect.left == -8); | 369 assert(rect.left == -8); |
| 358 {% endhighlight %} | 370 {% endhighlight %} |
| 359 | 371 |
| 360 With getters and setters, you can start | 372 With getters and setters, you can start |
| 361 with instance variables, | 373 with instance variables, |
| 362 later wrapping them with methods, | 374 later wrapping them with methods, |
| 363 all without changing client code. | 375 all without changing client code. |
| 364 | 376 |
| 365 <section id="classes-operators" markdown="1"> | 377 <section id="classes-operators" markdown="1"> |
| 366 #### Operators | 378 #### Operators as methods |
| 367 | 379 |
| 368 Because operators are just instance methods with special names, | 380 Because operators are just instance methods with special names, |
| 369 you can override [many operators](#op-methods). | 381 you can override [many operators](#op-methods). |
| 370 Here's an example of a class that overrides the `+` and `-` operators. | 382 Here's an example of a class that overrides the `+` and `-` operators. |
| 371 | 383 |
| 372 {% highlight dart %} | 384 {% highlight dart %} |
| 373 class Vector { | 385 class Vector { |
| 374 final int x,y; | 386 final int x,y; |
| 375 const Vector(this.x, this.y); | 387 const Vector(this.x, this.y); |
| 376 | 388 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 392 main() { | 404 main() { |
| 393 final v = new Vector(2,3); | 405 final v = new Vector(2,3); |
| 394 final w = new Vector(2,2); | 406 final w = new Vector(2,2); |
| 395 | 407 |
| 396 assert(v.x == 2 && v.y == 3); // v == (2,3) | 408 assert(v.x == 2 && v.y == 3); // v == (2,3) |
| 397 assert((-v).x == -2 && (-v).y == -3); // -v == (-2,-3) | 409 assert((-v).x == -2 && (-v).y == -3); // -v == (-2,-3) |
| 398 assert((v+w).x == 4 && (v+w).y == 5); // v+w == (4,5) | 410 assert((v+w).x == 4 && (v+w).y == 5); // v+w == (4,5) |
| 399 assert((v-w).x == 0 && (v-w).y == 1); // v-w == (0,1) | 411 assert((v-w).x == 0 && (v-w).y == 1); // v-w == (0,1) |
| 400 }{% endhighlight %} | 412 }{% endhighlight %} |
| 401 | 413 |
| 402 <aside class="note" markdown="1"> | 414 <aside> |
| 403 **Implementation note:** | 415 <div class="alert alert-info"> |
| 404 Overriding `-` affects only the binary subtraction operator. | 416 <strong>Tip:</strong> |
| 405 To override the unary form of `-`, | 417 Overriding <code>-</code> affects only the binary subtraction operator. |
| 406 you must use the special identifier **negate**. | 418 To override the unary form of <code>-</code>, |
| 419 you must use the special identifier <b>negate</b>. |
| 420 </div> |
| 407 </aside> | 421 </aside> |
| 408 | 422 |
| 409 </section> | 423 </section> |
| 410 </section> | 424 </section> |
| 411 | 425 |
| 412 | 426 |
| 413 ### Abstract classes {#classes-abstract} | 427 ### Abstract classes {#classes-abstract} |
| 414 | 428 |
| 415 Dart supports abstract classes and methods. | 429 Dart supports abstract classes and methods. |
| 416 Use the **abstract** keyword whenever you | 430 Use the **abstract** keyword whenever you |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 } | 530 } |
| 517 | 531 |
| 518 main() { | 532 main() { |
| 519 var a = new Point(2, 2); | 533 var a = new Point(2, 2); |
| 520 var b = new Point(4, 4); | 534 var b = new Point(4, 4); |
| 521 var distance = Point.distanceBetween(a,b); | 535 var distance = Point.distanceBetween(a,b); |
| 522 assert(distance < 2.9 && distance > 2.8); | 536 assert(distance < 2.9 && distance > 2.8); |
| 523 } | 537 } |
| 524 {% endhighlight %} | 538 {% endhighlight %} |
| 525 | 539 |
| 526 <aside class="note" markdown="1"> | 540 <aside> |
| 527 **Best practice:** Consider using top-level functions, | 541 <div class="alert alert-info"> |
| 528 instead of static methods, for common or widely | 542 <strong>Tip:</strong> |
| 529 used utilities and functionality. | 543 Consider using top-level functions, |
| 544 instead of static methods, for common or widely |
| 545 used utilities and functionality. |
| 546 </div> |
| 530 </aside> | 547 </aside> |
| 531 | 548 |
| 532 #### Static variables | 549 #### Static variables |
| 533 | 550 |
| 534 Static variables (class variables) are useful for class-wide state and | 551 Static variables (class variables) are useful for class-wide state and |
| 535 constants. | 552 constants. |
| 536 | 553 |
| 537 {% highlight dart %} | 554 {% highlight dart %} |
| 538 class Color { | 555 class Color { |
| 539 static final RED = const Color('red'); | 556 static final RED = const Color('red'); |
| 540 final String name; | 557 final String name; |
| 541 const Color(this.name); | 558 const Color(this.name); |
| 542 } | 559 } |
| 543 | 560 |
| 544 main() { | 561 main() { |
| 545 assert(Color.RED.name == 'red'); | 562 assert(Color.RED.name == 'red'); |
| 546 } | 563 } |
| 547 {% endhighlight %} | 564 {% endhighlight %} |
| OLD | NEW |