Example #1
0
        // 重载 + operator 来增加两个 Box 对象
        public static OperateReload operator +(OperateReload b, OperateReload c)
        {
            OperateReload box = new OperateReload();

            box.length  = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height  = b.height + c.height;
            return(box);
        }
Example #2
0
        public void Run()
        {
            if (flag)
            {
                Console.WriteLine("--------------Function overloading--------------");
                PrintInfo PL = new PrintInfo();
                PL.Print(1001);
                PL.Print(12.13);
                PL.Print("string type");

                Console.WriteLine("--------------Operator overloading--------------");
                OperateReload Box1   = new OperateReload(); // 声明 box1 为 box 类型
                OperateReload Box2   = new OperateReload(); // 声明 box2 为 box 类型
                OperateReload Box3   = new OperateReload(); // 声明 box3 为 box 类型
                double        volume = 0.0;                 // 在这里存放 box 的体积
                // box 1
                Box1.setLength(6.0);
                Box1.setBreadth(7.0);
                Box1.setHeight(5.0);
                // box 2
                Box2.setLength(12.0);
                Box2.setBreadth(13.0);
                Box2.setHeight(10.0);
                // box 1 体积
                Console.WriteLine("Volume of Box1 : {0}", Box1.getVolume());
                //  box 2 体积
                volume = Box2.getVolume();
                Console.WriteLine("Volume of Box2 : {0}", volume);
                // 将两个对象相加如下:
                Box3 = Box1 + Box2;
                // box 3 的体积
                volume = Box3.getVolume();
                Console.WriteLine("Volume of Box3 : {0}", volume);

                Console.WriteLine("--------------Abstract classes--------------");
                Abstract Abs = new Abstract(10, 5);
                Console.WriteLine(Abs.area());

                Console.WriteLine("--------------virtual functions--------------");
                Caller    c = new Caller();
                Rectangle r = new Rectangle(10, 7);
                Triangle  t = new Triangle(10, 5);
                c.CallArea(r);
                c.CallArea(t);

                Console.ReadKey();
            }
        }