OLD | NEW |
(Empty) | |
| 1 import 'dart:math' show Random; // Import a class from a library. |
| 2 |
| 3 void main() { // This is where the app starts executing
. |
| 4 print(new Die(n: 12).roll()); // Print a new object's value. Chain met
hod calls. |
| 5 } |
| 6 |
| 7 class Die { // Define a class. |
| 8 static Random shaker = new Random(); // Define a class variable. |
| 9 int sides, value; // Define instance variables. |
| 10 |
| 11 String toString() => '$value'; // Define a method using shorthand syntax
. |
| 12 |
| 13 Die({int n: 6}) { // Define a constructor. |
| 14 if (4 <= n && n <= 20) { |
| 15 sides = n; |
| 16 } else { |
| 17 throw new ArgumentError(/* */); // Support for errors and exceptions. |
| 18 } |
| 19 } |
| 20 int roll() { // Define an instance method. |
| 21 return value = shaker.nextInt(sides); // Get a random number. |
| 22 } |
| 23 } |
OLD | NEW |