示例#1
0
        public static distance operator +(distance D1, distance D2)
        {
            distance D3 = new distance();

            D3.meters = D1.meters + D2.meters;
            return(D3);
        }
示例#2
0
        public static void polymorphism()
        {
            /*  1. Static Polymorphism
             *      -1.1 Method Overloading
             *      -1.2 Operator Overloading
             *           -1.2.1 Unary Operators
             *           -1.2.2 Binary Operators
             *           -1.2.3 Comparison Operators
             *  2. Dynamic Polymorphism
             *      -2.1 Virtual method
             *      -2.2 Abstract method
             */
            //1.1 Method Overloading
            distance Distance1 = new distance();
            distance Distance2 = new distance(5); //the constructor method is overloaded

            //-1.2.1 Unary Operators
            distance Distance3 = Distance1++;

            Console.WriteLine("The Distance incremented is {0}", Distance3.meters);

            //-1.2.2 Binary Operators
            distance Distance4 = Distance2 + Distance3;

            Console.WriteLine("The sum of distance 1 and 2 is {0}", Distance4.meters);

            //-1.2.3 Comparison Operators
            bool flag = Distance2 > Distance3;

            //-2.1 Virtual method
            figure figure1 = new circle();

            Console.WriteLine("the area from the override method is {0}", figure1.getArea());

            //-2.2 Abstract method
            vehicle Vehicle = new bike();

            Console.WriteLine("the ringzise is {0}", Vehicle.ringsize());
        }