public static void Main() { Car car1 = new Car(); Motorcycle moto1 = new Motorcycle(); Cessna cessna1 = new Cessna(); Helicopter heli1 = new Helicopter(); JetSki jetski1 = new JetSki(); FishingBoat fish1 = new FishingBoat(); // Build a collection of all vehicles that fly List <IAirCraft> flyingVehicles = new List <IAirCraft>() { }; flyingVehicles.Add(cessna1); flyingVehicles.Add(heli1); // With a single `foreach`, have each vehicle Fly() foreach (IAirCraft vehicle in flyingVehicles) { vehicle.Fly(); } // Build a collection of all vehicles that operate on roads List <ILandCraft> landVehicles = new List <ILandCraft>() { }; landVehicles.Add(moto1); landVehicles.Add(car1); // With a single `foreach`, have each road vehicle Drive() foreach (ILandCraft vehicle in landVehicles) { vehicle.Drive(); } // Build a collection of all vehicles that operate on water List <IVehicle> waterVehicles = new List <IVehicle>() { }; waterVehicles.Add(jetski1); waterVehicles.Add(fish1); // With a single `foreach`, have each water vehicle Drive() foreach (IVehicle vehicle in landVehicles) { vehicle.Drive(); } }
static void Main(string[] args) { // Build a collection of all vehicles that fly Airplane cessna = new Airplane("propeller", 4, 150, true); Helicopter blackhawk = new Helicopter("Main rotor and Tail rotor", 6, 100, false); List <IairVehicle> airVehicles = new List <IairVehicle>(); airVehicles.Add(cessna); airVehicles.Add(blackhawk); // With a single `foreach`, have each vehicle Fly() foreach (var item in airVehicles) { var convertedItem = (Vehicle)item; Console.WriteLine($"my method of propulsion is {convertedItem.Propulsion} and I can carry {convertedItem.PassengerCapacity} passengers. {item.MaxAirSpeed}"); convertedItem.VehicleOperation(); // I can't reference any property or method that is specific to different classes (line 146-147) even if they are both of class Vehicle and interface IairVehicle } Console.WriteLine("\n"); // Build a collection of all vehicles that operate on roads Motorcycle CBR600RR = new Motorcycle("inline4 engine", 2, 180); Car subaru = new Car("inline 4 engie and AWD", 5, 120); List <IlandVehicle> landVehicles = new List <IlandVehicle>(); landVehicles.Add(CBR600RR); landVehicles.Add(subaru); // With a single `foreach`, have each road vehicle Drive() foreach (Vehicle item in landVehicles) { item.VehicleOperation(); } Console.WriteLine("\n"); // Build a collection of all vehicles that operate on water JetSki honda = new JetSki("water jet", 3, 35); Yatch allGold = new Yatch("twin water jet", 20, 30); List <Vehicle> waterVehicles = new List <Vehicle>(); waterVehicles.Add(honda); waterVehicles.Add(allGold); // With a single `foreach`, have each water vehicle Drive() foreach (Vehicle item in waterVehicles) { item.VehicleOperation(); } }
static void Main(string[] args) { Aeronca aeronca1 = new Aeronca(); Helicopter helicopter1 = new Helicopter(); List <IAirVehicle> flying = new List <IAirVehicle>(); flying.Add(aeronca1); flying.Add(helicopter1); foreach (var x in flying) { x.Fly(); x.Drive(); } Console.WriteLine(""); FourWheeler newFour = new FourWheeler(); Scooter newScoot = new Scooter(); List <ILandVehicle> land = new List <ILandVehicle>(); land.Add(newFour); land.Add(newScoot); foreach (var x in land) { x.Drive(); } Console.WriteLine(""); JetSki newSki = new JetSki(); Pontoon newToon = new Pontoon(); List <IWaterVehicle> water = new List <IWaterVehicle>(); water.Add(newSki); water.Add(newToon); foreach (var x in water) { x.Drive(); } }
public static void Main() { // Build a collection of all vehicles that fly List <IAirVehicle> aircraft = new List <IAirVehicle>(); Cessna c172 = new Cessna() { Winged = true, MaxAirSpeed = 239, Doors = 1, PassengerCapacity = 4, EngineVolume = 29, }; Helicopter lifeFlight = new Helicopter() { Winged = false, MaxAirSpeed = 200, Doors = 2, PassengerCapacity = 3, EngineVolume = 29, }; aircraft.Add(lifeFlight); aircraft.Add(c172); // With a single `foreach`, have each vehicle Fly() foreach (IAirVehicle a in aircraft) { a.Fly(); } // Build a collection of all vehicles that operate on roads List <ILandVehicle> landRovers = new List <ILandVehicle>(); Motorcycle Bmw = new Motorcycle() { MaxLandSpeed = 219, Doors = 0, PassengerCapacity = 2, TransmissionType = "Manual", EngineVolume = 20, Wheels = 2, }; Motorcycle Harley = new Motorcycle() { MaxLandSpeed = 180, Doors = 0, PassengerCapacity = 2, TransmissionType = "Manual", EngineVolume = 25, Wheels = 2, }; landRovers.Add(Harley); landRovers.Add(Bmw); // With a single `foreach`, have each road vehicle Drive() foreach (ILandVehicle l in landRovers) { l.Drive(); } // Build a collection of all vehicles that operate on water List <IWaterVehicle> waterCrafts = new List <IWaterVehicle>(); JetSki Kawasaki = new JetSki() { MaxWaterSpeed = 45.00, Doors = 0, PassengerCapacity = 1, EngineVolume = 10.00, }; Submarine u584 = new Submarine() { MaxWaterSpeed = 150.00, Doors = 2, PassengerCapacity = 6, EngineVolume = 2.00, }; waterCrafts.Add(Kawasaki); waterCrafts.Add(u584); // With a single `foreach`, have each water vehicle Drive() foreach (IWaterVehicle w in waterCrafts) { w.Drive(); } }
static void Main(string[] args) { // Build a collection of all vehicles that fly // With a single `foreach`, have each vehicle Fly() Airplane Boeing747 = new Airplane(); Helicopter Heli = new Helicopter(); Motorcycle Ninja = new Motorcycle(); Car Civic = new Car(); JetSki SeaDoo = new JetSki(); CruiseShip Viking = new CruiseShip(); List <IAirVehicle> FlyingVehicles = new List <IAirVehicle>() { Heli, Boeing747 }; List <IWaterVehicle> SailingVehicles = new List <IWaterVehicle>() { Viking, SeaDoo }; List <ILandVehicle> DrivingVehicle = new List <ILandVehicle>() { Civic, Ninja }; foreach (IAirVehicle Flyer in FlyingVehicles) { Console.WriteLine(Flyer.Fly()); } ; foreach (IWaterVehicle Sailer in SailingVehicles) { Console.WriteLine(Sailer.Drive()); } foreach (ILandVehicle Driver in DrivingVehicle) { Console.WriteLine(Driver.Drive()); } // Build a collection of all vehicles that operate on roads // With a single `foreach`, have each road vehicle Drive() // Build a collection of all vehicles that operate on water // With a single `foreach`, have each water vehicle Drive() }