Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var cars = new List <car>
            {
                new Audi(200, "green", "A4"),
                new BMW(25, "silver", "M3")
            };

            foreach (var car in cars)
            {
                car.Repair();
            }

            car bmwZ3  = new BMW(200, "black", "Z3");
            car audiA3 = new Audi(100, "blue", "A3");

            bmwZ3.ShowDetails();
            audiA3.ShowDetails();

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

            bmwM5.ShowDetails();

            car carB = (car)bmwM5;

            carB.ShowDetails();

            Console.ReadKey();
        }
Ejemplo n.º 2
0
 public static void Print(Auto auto)
 {
     Console.WriteLine("**************** print method ****************");
     if (auto is Auto)
     {
         Console.WriteLine(auto.Color);
         Console.WriteLine(auto.Name);
     }
     if (auto is BMW)
     {
         BMW bMW = auto as BMW; // the same as the lin of the audi ounder
         Console.WriteLine($"{bMW.Id}\t{bMW.Name}\t{bMW.Color}");
         //Console.WriteLine(bMW.Name);
         //Console.WriteLine(bMW.Color);
     }
     if (auto is Audi)
     {
         Audi audi = (Audi)auto;//here it was from above
         Console.Write(audi.Id);
         Console.WriteLine();
         Console.Write(audi.Name);
         Console.WriteLine();
         Console.Write(audi.Color);
         Console.WriteLine();
     }
 }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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();

            BMW bwmM5 = new BMW(330, "White", "M5");

            bwmM5.ShowDetails();
            Audi a4 = new Audi(150, "Black", "A4");

            a4.ShowDetails();

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Auto[] autos = new Auto[3];
            int[]  array = new int[3];
            array[0] = 9;
            array[1] = 255;
            array[2] = 22;

            Auto auto = new Auto();

            auto.Name  = "Parent";
            auto.Color = "white";

            Auto auto1 = new BMW();//impliciet casting

            auto1.Color = "red";
            auto1.Name  = "bmw";
            //BMW bmwtemp = (BMW)auto1;//expliciet casting
            BMW bmwtemp = auto1 as BMW;//expliciet casting

            if (bmwtemp is BMW && bmwtemp != null)
            {
                bmwtemp.Id = 987;
            }

            Audi audi = new Audi
            {
                Color = "black",
                Id    = 123,
                Name  = "C8"
            };

            autos[0] = auto;
            autos[1] = bmwtemp;
            autos[2] = audi;
            foreach (var item in autos)
            {
                Console.WriteLine(item);
                Print(item);
            }

            Console.WriteLine(bmwtemp.Id);
            Console.WriteLine(bmwtemp.Name);
            Console.WriteLine(bmwtemp.Color);

            Console.WriteLine();
            Console.WriteLine("**********************");
            Console.WriteLine();
            Print(auto);
            Print(bmwtemp);
            Print(audi);
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            var cars = new List <Car>
            {
                new Audi(200, "Blue", "A4"),
                new BMW(250, "Red", "M4")
            };

            foreach (var car in cars)
            {
                car.Repair();
            }

            Car bmw2  = new BMW(200, "Black", "Z3");
            Car audi2 = new Audi(100, "Black", "A3");

            bmw2.ShowDetails();
            audi2.ShowDetails();

            BMW bmw3 = new BMW(300, "Green", "M5");

            bmw3.ShowDetails();

            Car carB = (Car)bmw3;

            carB.ShowDetails();

            M3 myM3 = new M3(260, "Red", "M3extraGreat");

            myM3.Repair();

            bmw2.setCarIDInfo(1, "Christopher Wuydts");
            audi2.setCarIDInfo(2, "Christopher Wuydts");
            bmw2.getCarIDInfo();
            audi2.getCarIDInfo();
        }