コード例 #1
0
ファイル: Program.cs プロジェクト: psgens/C-BootCamp
        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();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: ivanbirch/Polymorphism
        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();
        }
コード例 #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();

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

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

            a4.ShowDetails();

            Console.ReadKey();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: DonTerrax/SKP
        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();
        }
コード例 #5
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();
        }