OLD | NEW |
---|---|
(Empty) | |
1 import 'dart:math' as math; // Importing a library. | |
2 | |
3 // Functions and variables can be inside or outside of classes. | |
4 var _shapes = new List(); // A global variable. | |
sethladd
2013/08/09 01:45:22
I think we can drop the _, as I wonder if it's too
Kathy Walrath
2013/08/12 18:30:56
Done.
| |
5 addShape(shape) => _shapes.add(shape); // Function shorthand syntax: => | |
6 | |
7 class Ellipse extends Shape { // Declaring a class. | |
8 num major = 0; // An instance variable (property). | |
9 num minor = 0; | |
10 static const _C = math.PI/4; // A constant. | |
11 num get area => _C*major*minor; // A property implemented with a getter. | |
12 | |
13 Ellipse(this.major, this.minor); // Compact constructor syntax. | |
14 Ellipse.golden(this.major) { // A named constructor. | |
15 minor = major*(1 + math.sqrt(5))/2; | |
16 } | |
17 | |
18 // HIDE THIS: Overriding Object's toString() method. | |
19 String toString() => 'Ellipse: ${major}x${minor} ($area); rotation: $rotation; $color'; | |
20 } | |
21 | |
22 // Every app has a main() function, where execution starts. | |
23 main() { | |
24 print(new Ellipse.golden(1)); | |
25 | |
26 // The cascade operator (..) saves you from repetitive typing. | |
27 addShape(new Ellipse.golden(10)..minor *= 2 | |
28 ..rotation = 20 | |
29 ..color = 'rgb(0,129,198)'); | |
30 | |
31 print(_shapes[0]); | |
32 } | |
33 | |
34 abstract class Shape { | |
35 num get area; | |
36 num rotation = 0; | |
37 String color = 'black'; | |
38 } | |
OLD | NEW |