OLD | NEW |
---|---|
1 import 'dart:math'; // Import a library. | 1 import 'dart:math' show PI; |
2 | 2 |
3 List shapes = []; // Use literals to create lists. | |
4 addShape(shape) => shapes.add(shape); // Function shorthand syntax. | |
5 | |
6 main() { | |
7 // The cascade operator (..) saves you from repetitive typing. | |
8 addShape(new Ellipse(10, 20)..rotation = 45*PI/180 | |
9 ..color = 'rgb(0,129,198)' | |
10 ..outlineWidth = 0); | |
11 | |
12 // You can easily insert expression values into strings. | |
13 print('Area of the first shape: ${shapes[0].area}'); | |
14 } | |
15 | |
3 abstract class Shape { | 16 abstract class Shape { |
Kathy Walrath
2013/11/23 00:46:27
Why is this still in here?
Oh... I didn't notice
| |
4 num get area; | 17 num get area; |
5 num rotation = 0; | 18 num rotation = 0; |
6 num outlineWidth = 1; | 19 num outlineWidth = 1; |
7 String color = 'black'; | 20 String color = 'black'; |
8 } | 21 } |
9 | 22 |
10 class Ellipse extends Shape { // Declare a class. | 23 class Ellipse extends Shape { |
11 num majorAxis = 0; // An instance variable (property). | 24 num minorAxis, majorAxis; |
12 num minorAxis = 0; | 25 |
26 // Syntactic sugar to set members before the constructor body runs. | |
27 Ellipse(this.minorAxis, this.majorAxis); | |
28 | |
13 static const num C = PI/4; // A constant. | 29 static const num C = PI/4; // A constant. |
Kathy Walrath
2013/11/23 00:46:27
Get rid of code that isn't already on the front pa
| |
14 num get area => C*majorAxis*minorAxis; // A property implemented with a gett er. | 30 num get area => C*majorAxis*minorAxis; // A property implemented with a gette r. |
Kathy Walrath
2013/11/23 00:46:27
Should show this on the front page. It seems like
| |
15 | 31 |
16 Ellipse(this.majorAxis, this.minorAxis); // Compact constructor syntax. | 32 Ellipse.circle(diameter) { // A named constructor. |
17 Ellipse.circle(diameter) { // A named constructor. | |
18 minorAxis = majorAxis = diameter; | 33 minorAxis = majorAxis = diameter; |
19 } | 34 } |
20 | 35 |
21 // Override Object's toString() method. | 36 // Override Object's toString() method. |
22 String toString() => | 37 String toString() => |
23 'Ellipse: ${majorAxis}x${minorAxis} ($area); rotation: $rotation; $color'; | 38 'Ellipse: ${majorAxis}x${minorAxis} ($area); rotation: $rotation; $color'; |
24 } | 39 } |
25 | |
26 // Functions and variables can be inside or outside of classes. | |
27 var shapes = new List(); // A global variable. | |
28 addShape(shape) => shapes.add(shape); // Function shorthand syntax. | |
29 | |
30 // Every app has a main() function, where execution starts. | |
31 main() { | |
32 // The cascade operator (..) saves you from repetitive typing. | |
33 addShape(new Ellipse(10, 20)..rotation = 45*PI/180 | |
34 ..color = 'rgb(0,129,198)' | |
35 ..outlineWidth = 0); | |
36 | |
37 // Convert expressions to strings using ${...}. | |
38 print('Area of the first shape: ${shapes[0].area}'); | |
39 print(shapes[0]); | |
40 } | |
OLD | NEW |