OLD | NEW |
(Empty) | |
| 1 import 'dart:math'; // Import a library. |
| 2 |
| 3 abstract class Shape { |
| 4 num get area; |
| 5 num rotation = 0; |
| 6 num outlineWidth = 1; |
| 7 String color = 'black'; |
| 8 } |
| 9 |
| 10 class Ellipse extends Shape { // Declare a class. |
| 11 num majorAxis = 0; // An instance variable (property). |
| 12 num minorAxis = 0; |
| 13 static const num C = PI/4; // A constant. |
| 14 num get area => C*majorAxis*minorAxis; // A property implemented with a gett
er. |
| 15 |
| 16 Ellipse(this.majorAxis, this.minorAxis); // Compact constructor syntax. |
| 17 Ellipse.circle(diameter) { // A named constructor. |
| 18 minorAxis = majorAxis = diameter; |
| 19 } |
| 20 |
| 21 // Override Object's toString() method. |
| 22 String toString() => |
| 23 'Ellipse: ${majorAxis}x${minorAxis} ($area); rotation: $rotation; $color'; |
| 24 } |
| 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 |