Exemple #1
0
        //The following is the exercise he gave us in the video:

        // Create a base class Car with two properties HP and Color
        // Create a Constructor setting those two properties
        // Create a Method called ShowDetails() which shows the HP and Color of the car on the console
        // Create a Repair Method which writes "Car was repaired!" onto the console

        /* Create two deriving classes, BMW and Audi, which have their own constructor and have an aditional property
         * called Model. Also a private member called brand. Brand should be different in each of the two classes.*/
        // Create the two methods ShowDetails() and Repair in them as well. Adjust those methods accordingly.

        static void Main(string[] args)
        {
            var cars = new List <car>
            {
                new Audi("A4", 200, "Blue"),
                new BMW("M3", 200, "Blue")
            };//This displays 1 form of polymorphism, YOU CAN USE CLASSES WITH LISTS!

            foreach (var car in cars)
            {
                //The virtual method is invoked via sub-classes
                car.Repair();
            }

            car m5 = new BMW("M5", 200, "Green"); //car class (base class)
            BMW m7 = new BMW("M7", 450, "Gray");  //BMW class

            m5.ShowDetails();                     //Output is of car ShowDetail() method
            m7.ShowDetails();                     //Output is of BMW ShowDetail() method

            car carB = (car)m7;                   //Casting a class, subclass->baseclass

            carB.ShowDetails();                   //Car B is a base class, it's no longer a BMW.

            M3 myM3 = new M3("M3 Super", 350, "Silver");

            myM3.Repair();

            myM3.SetCarIdInfo(1234, "Sean Mongru");//Displaying ' Has A ' Relationships
            myM3.GetCarIdInfo();

            Console.ReadKey();
        }
Exemple #2
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();
        }
        // pre-challenge:
        // create a base class Car with two properties HP and Color (done)
        // create a constructor setting those two properties (done)
        // create a method called ShowDetails() which shows the HP and Color of the car on the console (done)
        // create a Repair() method which writes "Car was repaired!" onto the console (done)
        // create two deriving class, BMW and Audi, which have their own constructor and have an additional property (done)
        // called Model. Also a private member called brand. Brand should be different in each of the two classes. (done)
        // create the two methods ShowDetails() and Repair() in them as well. Adjust those methods accordingly (done)
        static void Main(string[] args)
        {
            // use polymorphism to create a list of cars
            // List<> is found in System.Collection.Generic namespace

            // a car can be a BMW, an Audi, a Porsche etc.
            // Polymorphism at work #1: an Audi, BMW, Porsche
            // can all be used whenever a Car is expected. No cast is
            // required because an implicit conversion exists from a derived
            // class to its base class
            var cars = new List <Car>
            {
                new Audi(200, "blue", "A4"),
                new BMW(250, "red", "M3")
            };

            // Polymorphism 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();
            }

            // show the "new" showDetails() method from derived BMW class
            BMW bmwM5 = new BMW(330, "white", "M5");

            bmwM5.ShowDetails();

            // use type casting to convert bmwM5 to use base class methods
            Car carB = (Car)bmwM5;

            carB.ShowDetails();

            // create an object of M3
            M3 myM3 = new M3(260, "red", "m3Super Turbo");

            myM3.Repair();

            // demonstrate car info that has a relationship with CarIDInfo
            myM3.SetCarIDInfo(15689, "Jordan");
            myM3.GetCarIDInfo();


            Console.ReadKey();
        }
Exemple #4
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();
        }
Exemple #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();
        }