public static void Main() { // Build a collection of all vehicles that fly // With a single `foreach`, have each vehicle Fly() Blimp blimp1 = new Blimp(); Cessna cessna1 = new Cessna(); List <IAirBased> thingsThatFly = new List <IAirBased>(); thingsThatFly.Add(blimp1); thingsThatFly.Add(cessna1); foreach (IAirBased thing in thingsThatFly) { thing.Fly(); } // Build a collection of all vehicles that operate on roads // With a single `foreach`, have each road vehicle Drive() Motorcycle motorcycle1 = new Motorcycle(); PickupTruck truck1 = new PickupTruck(); List <IVehicle> thingsOnRoad = new List <IVehicle>(); thingsOnRoad.Add(motorcycle1); thingsOnRoad.Add(truck1); foreach (IVehicle thing in thingsOnRoad) { thing.Drive(); } // Build a collection of all vehicles that operate on water // With a single `foreach`, have each water vehicle Drive() JetSki jet1 = new JetSki(); Pontoon pon1 = new Pontoon(); List <IVehicle> thingsOnWater = new List <IVehicle>(); thingsOnWater.Add(jet1); thingsOnWater.Add(pon1); foreach (IVehicle thing in thingsOnWater) { thing.Drive(); } }
public static void Main() { Cessna myPlane = new Cessna(); Boing747 myTransport = new Boing747(); // Build a collection of all vehicles that fly List <IAirCraft> flyingVics = new List <IAirCraft>(); flyingVics.Add(myPlane); flyingVics.Add(myTransport); // With a single `foreach`, have each vehicle Fly() foreach (var vic in flyingVics) { vic.Fly(); } Motorcycle harly = new Motorcycle(); PickupTruck f150 = new PickupTruck(); // Build a collection of all vehicles that operate on roads List <ILandCraft> roadVics = new List <ILandCraft> { harly, f150 }; // With a single `foreach`, have each road vehicle Drive() foreach (var vic in roadVics) { vic.Drive(); } Cessna privateJet = new Cessna(); Boing747 passengerJet = new Boing747(); // Build a collection of all vehicles that operate on water List <IAirCraft> airVics = new List <IAirCraft>(); airVics.Add(privateJet); airVics.Add(passengerJet); // With a single `foreach`, have each water vehicle Drive() foreach (var vic in airVics) { vic.Fly(); } }