Example #1
0
    public static void Main()
    {
        Bicycle bicycle = new Bicycle();

        bicycle.ChangeGear(2);
        bicycle.speedUp(3);
        bicycle.applyBrakes(1);
        Console.WriteLine("bicyle present state:");
        bicycle.presentState();

        Console.WriteLine("--------------");

        Bike bike = new Bike;

        bike.ChangeGear(2);
        bike.speedUp(3);
        bike.applyBrakes(1);
        Console.WriteLine("bike present state:");
        bike.presentState();
        Console.ReadLine();
    }
        private static void CallInterfaceClassToExample3Output()
        {
            // creating an instance of Bicycle
            // doing some operations
            Bicycle bicycle = new Bicycle();

            bicycle.ChangeGear(2);
            bicycle.SpeedUp(3);
            bicycle.ApplyBrakes(1);

            Console.WriteLine("Bicycle present state :");
            bicycle.printStates();

            // creating instance of bike.
            Bike bike = new Bike();

            bike.ChangeGear(1);
            bike.SpeedUp(4);
            bike.ApplyBrakes(3);

            Console.WriteLine("Bike present state :");
            bike.printStates();
        }
Example #3
0
        static void Main(string[] args)
        {
            try
            {
                //Using interface IVehicle as our variable type
                IVehicle myBike     = new Bicycle();
                IVehicle myCar      = new Car();
                IVehicle anotherCar = new Car();

                myBike.Make  = "Giantt";
                myBike.Model = "XTC Advanced 29";
                myBike.ChangeGear(15); //Polymorphic call - at runtime it will determine which method to call based on the class in the variable
                myBike.SpeedUp(3);     //Polymorphic

                myCar.Make  = "Jeep";
                myCar.Model = "Wrangler";
                myCar.ChangeGear(2); //polymorphic
                myCar.SpeedUp(15);   //polymorphic

                if (myBike is Bicycle)
                {
                    ((Bicycle)myBike).DeployKickStand();
                }

                List <IVehicle> myRides = new List <IVehicle>()
                {
                    myBike, myCar, anotherCar
                };

                List <IPrintable> myVehicles = new List <IPrintable>()
                {
                    (IPrintable)myBike,
                    (IPrintable)myCar,
                    (IPrintable)anotherCar
                };

                foreach (IPrintable p in myVehicles)
                {
                    P(p.Print() + Environment.NewLine);

                    if (p is Bicycle)
                    {
                        P("This is a bike.\n");
                    }

                    if (p is Car)
                    {
                        P("This is a car.\n");
                    }
                }

                foreach (IVehicle v in myRides)
                {
                    P($"{v.Make} \t\t{v.Model}");
                    if (v is Car)
                    {
                        v.ChangeGear(0); //Polymorphic
                    }
                    if (v is Bicycle)
                    {
                        P("This is a bike.\n");
                    }

                    if (v is Car)
                    {
                        P("This is a car.\n");
                    }

                    P(((IPrintable)v).Print());//Polymorphic
                }

                foreach (IVehicle v in myRides.Where(r => r is Car))
                {
                    P($"{v.Make} \t\t{v.Model}");
                }
            }
            catch (Exception ex)
            {
                P(ex.Message);
            }

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            try
            {
                // Using interface as variable types
                IVehicle myBike     = new Bicycle();
                IVehicle myCar      = new Car();
                IVehicle myOtherCar = new Car();

                myBike.Make  = "Giant";
                myBike.Model = "XTC Advanced 29";
                myBike.SpeedUp(3);
                myBike.ChangeGear(15);

                if (myBike is Bicycle)// type check to ensure a safe cast
                {
                    ((Bicycle)myBike).DeployKiskStand();
                    P("Kick stand deployed: " + ((Bicycle)myBike).KickStandDown);
                }

                myCar.Make  = "Jeep";
                myCar.Model = "Wrangler";
                myCar.SpeedUp(-3);

                myOtherCar.Make  = "Forester";
                myOtherCar.Model = "Subaru";
                myOtherCar.ChangeGear(3);
                myOtherCar.SpeedUp(30);

                List <IVehicle> myVehicles = new List <IVehicle>()
                {
                    myBike, myCar, myOtherCar
                };

                foreach (IVehicle v in myVehicles)
                {
                    //Print
                    IPrintable myVehicle = null;

                    if (v is Car)
                    {
                        myVehicle = (Car)v;
                    }

                    if (v is Bicycle)
                    {
                        myVehicle = (Bicycle)v;
                    }

                    P("--------------------------------------");
                    if (myVehicle != null)
                    {
                        P(myVehicle.Print());
                    }
                }
            }
            catch (Exception ex)
            {
                P(ex.Message);
            }

            Console.ReadLine(); // Haults closing
        }