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(); }
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(); } }
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(); }
// 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 BMW("orange", 200, "M3", "family"), new Audi("yellow", 400, "A4", "sport") }; foreach (Car item in cars) { if (item is Car) { Console.WriteLine("\n" + item + " is a Car"); if (item is BMW) { Console.WriteLine(item + " is a BMW"); } if (item is Audi) { Console.WriteLine(item + " is a Audi"); } } Console.WriteLine(item.GetType()); item.Repair(); item.ShowDetails(); Console.WriteLine("---------------------\n"); } Car b23 = new BMW("white", 23, "channel", "rich"); b23.SetCarIDInfo(301, "Drake"); b23.GetInfoCar(); }
//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) { //base can call any function of deriving class Car Bmw = new BMW(800, "290i", "grey", "BMW"); Bmw.showDetails(); Bmw.showDetailz(); //Create a list of cars var cars = new List <Car> { new BMW(2000, "Yellow", "320i", "BMW"), new Audi(700, "Green", "RS7", "Audi") }; //loop through list to get showdetails() of each object foreach (var ride in cars) { //ride.showDetails(); } Car Benz = new Car(300, "Purple"); //Benz.CarDetails(); BMW benz = new BMW(450, "Blue", "C63", "Merc"); //benz.CarDetails(); //to show content of base class on deriving member Car carb = (Car)benz; carb.CarDetails(); //######## - SEALED - ###### //Rs7_subclassAudi_.cs Rs7_subclassAudi_ Rs7 = new Rs7_subclassAudi_(890, "Indigo", "RS77", "S-Line"); Rs7.showNews(); //Has A Relationship //Works on oly new classes created here Bmw.SetCarID(32, "Mr. Amponsah"); Bmw.GetCarIDinfo(); Console.ReadKey(); }
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); }
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(); }
static void Main(string[] args) { var cars = new System.Collections.Generic.List <Car> { new Audi(200, "Blue", "A4"), new BMW(250, "Grey", "320") }; foreach (var car in cars) { car.showDetails(); car.repair(); } Car BMWZ3 = new BMW(200, "black", "z3"); BMWZ3.showDetails(); BMWZ3.SetCarIdInfo(1234, "Me"); BMWZ3.GetCaridInfo(); }