Beispiel #1
0
        static void Main(string[] args)
        {
            var cars = new List <Car>
            {
                new Audi(200, "blue", "A4"),
                new BMW(250, "red", "M3")
            };

            foreach (var car in cars)
            {
                car.Repair();
            }
            Car bmwZ3  = new BMW(200, "black", "Z3");
            Car audiA3 = new Audi(100, "green", "A3");

            bmwZ3.ShowDetails();
            audiA3.ShowDetails();
            bmwZ3.SetCarIDInfo(1234, "me");
            audiA3.SetCarIDInfo(1235, "Frank");
            bmwZ3.GetCarIDInfo();
            audiA3.GetCarIDInfo();

            BMW bmwM5 = new BMW(330, "white", "M5");

            bmwM5.ShowDetails();

            Car carB = (Car)bmwM5;

            carB.ShowDetails();

            M3 myM3 = new M3(260, "red", "M3");

            myM3.Repair();
            Console.ReadKey();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // A car can be a Bmw,Audi or Porche etc.
            // Polymorphism at work #1 an Audi, Bmw, Porche
            // can all be used whereever a car is expected. No cast is required
            // because an implicit conversion exists from a dreived class to its base class.
            var cars = new List <Car>
            {
                new Audi(420, "red", "RS3"),
                new Bmw(450, "blue", "M3")
            };


            // Polymopishm at work#2 The virtual method repair is invoked on each of the derived classes, not the base class.
            foreach (var car in cars)
            {
                car.Repair();
            }

            Car BmwM4   = new Bmw(500, "Black", "M4");
            Car AudiRS6 = new Audi(550, "Nardo Gray", "RS6");

            BmwM4.ShowDetails();
            AudiRS6.ShowDetails();

            BmwM4.SetCarIDInfo(1234, "Mike");
            AudiRS6.SetCarIDInfo(12345, "Morten");
            BmwM4.GetCarIDInfo();
            AudiRS6.GetCarIDInfo();

            Bmw BmwM5 = new Bmw(600, "Red", "M5");

            BmwM5.ShowDetails();

            M3 myM3 = new M3(350, "red", "M3 super");

            myM3.Repair();
        }