Die-variants/die-copy-constructor/die.cs - The class Die with a copy constructor. | Lecture 3 - slide 28 : 29 Program 1 |
using System; public class Die { private int numberOfEyes; private Random randomNumberSupplier; private readonly int maxNumberOfEyes; public Die (Die d){ numberOfEyes = d.numberOfEyes; randomNumberSupplier = d.randomNumberSupplier; maxNumberOfEyes = d.maxNumberOfEyes; } public Die (): this(6){} public Die (int maxNumberOfEyes){ randomNumberSupplier = new Random(unchecked((int)DateTime.Now.Ticks)); this.maxNumberOfEyes = maxNumberOfEyes; numberOfEyes = randomNumberSupplier.Next(1,maxNumberOfEyes + 1); } public void Toss (){ numberOfEyes = randomNumberSupplier.Next(1,maxNumberOfEyes + 1); } public int NumberOfEyes() { return numberOfEyes; } public override String ToString(){ return String.Format("Die[{0}]: {1}", maxNumberOfEyes, numberOfEyes); } }