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(); }
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(); }
//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(); }
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(); }
static void Main(string[] args) { // a car can be BMW, and Audi, a Porsche etc. // Polymorphise at work #1: Audi, BMW, Porsche // can all be used wherever 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("Blue", "A4", 200), new BMW("Black", "T3", 100) }; // 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(); } Car bmwZ3 = new BMW("Black", "Z3", 200); Car audiA1 = new BMW("Red", "A1", 300); bmwZ3.ShowDetails(); audiA1.ShowDetails(); bmwZ3.SetCarIDinfo(123, "Daniel Muñoz"); audiA1.SetCarIDinfo(534, "Frank"); bmwZ3.GetCarIDInfo(); audiA1.GetCarIDInfo(); BMW bmwM5 = new BMW("Red", "M5", 200); bmwM5.ShowDetails(); bmwM5.SetCarIDinfo(5546, "Momo"); bmwM5.GetCarIDInfo(); // In case of wanting to show one of the derived class // instances as the base one you can Cast as below. Car carB = bmwM5; carB.ShowDetails(); //M3 m3 = new M3("Red", "M5", 200); //m3.Repair(); Console.ReadKey(); }
static void Main(string[] args) { List <Car> cars = new List <Car> { new Audi("white", "Sv300"), new BMW("black", "M5") }; foreach (Car car in cars) { car.ShowDetails(); } BMW bmwM5 = (BMW)cars[1]; bmwM5.ShowDetails(); }
// 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(); }
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(); }