Index: src/tests/site/code/syntax.dart |
diff --git a/src/tests/site/code/syntax.dart b/src/tests/site/code/syntax.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..322643e6bccc648c6b7f17a9dfd79a07ae30b80f |
--- /dev/null |
+++ b/src/tests/site/code/syntax.dart |
@@ -0,0 +1,38 @@ |
+import 'dart:math' as math; // Importing a library. |
+ |
+// Functions and variables can be inside or outside of classes. |
+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.
|
+addShape(shape) => _shapes.add(shape); // Function shorthand syntax: => |
+ |
+class Ellipse extends Shape { // Declaring a class. |
+ num major = 0; // An instance variable (property). |
+ num minor = 0; |
+ static const _C = math.PI/4; // A constant. |
+ num get area => _C*major*minor; // A property implemented with a getter. |
+ |
+ Ellipse(this.major, this.minor); // Compact constructor syntax. |
+ Ellipse.golden(this.major) { // A named constructor. |
+ minor = major*(1 + math.sqrt(5))/2; |
+ } |
+ |
+ // HIDE THIS: Overriding Object's toString() method. |
+ String toString() => 'Ellipse: ${major}x${minor} ($area); rotation: $rotation; $color'; |
+} |
+ |
+// Every app has a main() function, where execution starts. |
+main() { |
+ print(new Ellipse.golden(1)); |
+ |
+ // The cascade operator (..) saves you from repetitive typing. |
+ addShape(new Ellipse.golden(10)..minor *= 2 |
+ ..rotation = 20 |
+ ..color = 'rgb(0,129,198)'); |
+ |
+ print(_shapes[0]); |
+} |
+ |
+abstract class Shape { |
+ num get area; |
+ num rotation = 0; |
+ String color = 'black'; |
+} |