示例#1
0
        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, "Riyad");
            audiA3.SetCarIDInfo(12345, "Frank White");
            bmwZ3.GetCarIDInfo();
            audiA3.GetCarIDInfo();

            BMW bmwM5 = new BMW(330, "black", "M5");

            bmwM5.ShowDetails();

            Car carB = (Car)bmwM5;

            carB.ShowDetails();
            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            //Polymorphism at work #1: An Audi, BMW
            //can all be use whereever Car is expected. No cast
            //is required becuse 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, "Orange", "Z5")
            };

            //Polymorphism at work #2: the virtual method Repair
            // is invoded on each of the derived classes, not the base class
            //this is because we used a virtual Repair in the base class
            //and override the repairs in BMW and Audi
            foreach (var car in cars)
            {
                car.Repair();
            }
            //this show details is from the Car class
            Car bmwZ3   = new BMW(200, "black", "Z3");
            Car audieA3 = new Audi(125, "green", "A3");

            bmwZ3.ShowDetails();
            audieA3.ShowDetails();

            bmwZ3.SetCarIDInfo(1234, "Donald Mertz");
            audieA3.SetCarIDInfo(1235, "Larry Davis");
            bmwZ3.GerCarIDInfo();
            audieA3.GerCarIDInfo();

            //this show details is from the BMW class becuase we called a new BMW not Car
            BMW bmwM5 = new BMW(330, "white", "M5");

            bmwM5.ShowDetails();

            //this tells the compiler to use the Base Car showdetails
            Car carb = (Car)bmwM5;

            carb.ShowDetails();

            M3 myM3 = new M3(276, "red", "M3");

            myM3.Repair();
        }
示例#3
0
        // 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)
        {
            // a car can be a BMW, an Audi, a Porsche etc.
            // Polymorphism at work #1: an Audi,  BMW, Porsche
            // can all be used whereever 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();
            }

            Car bmwZ3  = new BMW(200, "black", "Z3");
            Car audiA3 = new Audi(100, "green", "A3");

            bmwZ3.ShowDetails();
            audiA3.ShowDetails();

            bmwZ3.SetCarIDInfo(1234, "Denis Panjuta");
            audiA3.SetCarIDInfo(1235, "Frank White");
            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 Super Turbo");

            myM3.Repair();
            Console.ReadKey();
        }
示例#4
0
        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.RepairCar();
            }

            Car bmwZ3  = new BMW(200, "black", "Z3");
            Car audiA3 = new Audi(100, "green", "A3");

            bmwZ3.ShowDetails();
            audiA3.ShowDetails();

            bmwZ3.SetCarIDInfo(1234, "Fabio C");
            audiA3.SetCarIDInfo(1235, "Rocky Tobias");
            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", "M3Super Turbo");

            myM3.RepairCar();

            Console.ReadKey();
        }
示例#5
0
        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, "Mayya");
            audiA3.SetCarIDInfo(2222, "Anna");

            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 Super Turbo");

            myM3.Repair();
        }
示例#6
0
        static void Main(string[] args)
        {
            Random dice = new Random();
            int    numEyes;

            for (int i = 0; i < 10; i++)
            {
                numEyes = dice.Next(0, 10);
                Console.WriteLine(numEyes);
            }

            int random = dice.Next(1, 4);

            if (random == 1)
            {
                Console.WriteLine("YES");
            }
            else if (random == 2)
            {
                Console.WriteLine("May be");
            }

            else
            {
                Console.WriteLine("NO");
            }


            DateTime dateTime = new DateTime(1995, 1, 8);

            Console.WriteLine(dateTime.DayOfWeek);
            Console.WriteLine(dateTime);
            Console.WriteLine(dateTime.Ticks);


            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, "Denis Panjuta");
            audiA3.SetCarIDInfo(1235, "Frank White");
            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 Super Turbo");

            myM3.Repair();

            //Shape shape = new Shape();
            Shape[] shapes = { new Sphere(4), new Cube(3) };
            foreach (Shape shape  in shapes)
            {
                Console.WriteLine(shape.Volume());
                Cube iceCube = shape as Cube;
                if (iceCube == null)
                {
                    Console.WriteLine("This is no cube");
                }

                if (iceCube is Cube)
                {
                    Console.WriteLine("This is a cube");
                }
            }
            Console.ReadKey();
        }