public static void Start()
        {
            Console.WriteLine("\nMultipleDispatchWithDynamic\n");

            SpaceShip ship = new SpaceShip() { Name = "enterprise" } ;
            IObjectInSpace a = new Planet() { Gravity = 1 };
            IObjectInSpace b = new Planet() { Gravity = 5 };
            IObjectInSpace c = new Asteroid() { Metal = 3 };
            IObjectInSpace d = new Asteroid() { Metal = 2 };

            Console.WriteLine(ship.DescribeInteraction(a));
            Console.WriteLine(ship.DescribeInteraction(b));
            Console.WriteLine(ship.DescribeInteraction(c));
            Console.WriteLine(ship.DescribeInteraction(d));

            // Downside: This would result in a StackOverflow because there is no method for UnknownObject
            // Console.WriteLine(ship.DescribeInteraction(new UnknownObject()));
        }
 public string DescribeInteraction(Asteroid other)
 {
     return "the " + Name + " mines " + other.Metal + " tons of metal from an asteroid";
 }
Ejemplo n.º 3
0
 public string DescribeInteraction(Asteroid other)
 {
     return("the " + Name + " mines " + other.Metal + " tons of metal from an asteroid");
 }