示例#1
0
        static void Main(string[] args)
        {
            // Build a collection of all vehicles that fly
            var Boeing737 = new Aircraft("blue", 150);
            var JumboJet  = new Aircraft("grey", 225);
            var UFO       = new Aircraft("shiny", 1);

            List <Aircraft> FlyingVehicles = new List <Aircraft>();

            FlyingVehicles.Add(Boeing737);
            FlyingVehicles.Add(JumboJet);
            FlyingVehicles.Add(UFO);

            // With a single `foreach`, have each vehicle Fly()
            foreach (Aircraft plane in FlyingVehicles)
            {
                plane.Fly();
            }

            // Build a collection of all vehicles that operate on roads
            var Camaro   = new Car("red", 2);
            var Mustang  = new Car("black", 4);
            var Corvette = new Car("grey", 2);

            List <Car> DrivingVehicles = new List <Car>();

            DrivingVehicles.Add(Camaro);
            DrivingVehicles.Add(Mustang);
            DrivingVehicles.Add(Corvette);

            // With a single `foreach`, have each road vehicle Drive()
            foreach (Car car in DrivingVehicles)
            {
                car.Drive();
            }

            // Build a collection of all vehicles that operate on water
            var Tugboat     = new Watercraft("black", 4);
            var Speedboat   = new Watercraft("green", 3);
            var Fishingboat = new Watercraft("silver", 15);

            List <Watercraft> WaterVehicles = new List <Watercraft>();

            WaterVehicles.Add(Tugboat);
            WaterVehicles.Add(Speedboat);
            WaterVehicles.Add(Fishingboat);

            // With a single `foreach`, have each water vehicle Drive()
            foreach (Watercraft boat in WaterVehicles)
            {
                boat.Drive();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            // Build a collection of all vehicles that fly
            // With a single `foreach`, have each vehicle Fly()
            var boeing = new Aircraft
            {
                PassengerOccupancy = 100,
                FuelCapacity       = 1000
            };

            var jet = new Aircraft
            {
                PassengerOccupancy = 10,
                FuelCapacity       = 100
            };

            var turboprop = new Aircraft
            {
                PassengerOccupancy = 3,
                FuelCapacity       = 200
            };

            var aircrafts = new List <Aircraft>();

            aircrafts.Add(boeing);
            aircrafts.Add(jet);
            aircrafts.Add(turboprop);

            foreach (var aircraft in aircrafts)
            {
                aircraft.Flying();
            }
            // Build a collection of all vehicles that operate on roads
            // With a single `foreach`, have each road vehicle Drive()

            var truck = new Car
            {
                PassengerOccupancy = 6,
                FuelType           = "Diesel"
            };

            var bus = new Car
            {
                PassengerOccupancy = 20,
                FuelType           = "Diesel"
            };

            var honda = new Car
            {
                PassengerOccupancy = 3,
                FuelType           = "Unleaded"
            };

            var cars = new List <Car>();

            cars.Add(truck);
            cars.Add(bus);
            cars.Add(honda);

            foreach (var car in cars)
            {
                car.Driving();
            }

            // Build a collection of all vehicles that operate on water
            // With a single `foreach`, have each water vehicle Drive()

            var jetSki = new Watercraft
            {
                PassengerOccupancy = 2,
                FuelCapacity       = 10
            };

            var dinghy = new Watercraft
            {
                PassengerOccupancy = 4,
                FuelCapacity       = 0
            };

            var watercrafts = new List <Watercraft>();

            watercrafts.Add(jetSki);
            watercrafts.Add(dinghy);

            foreach (var watercraft in watercrafts)
            {
                watercraft.Driving();
            }
        }