编辑代码

using System;
namespace OperatorOvApplication
{
    class Box
    {
        private double length;    //长度
        private double breadth;   //宽度
        private double height;    //高度

        public double getVolume()    //获取体积
        {
            return length*breadth*height;
        }

        public void setLength(double len)   //设置长度
        {
            length = len;
        }

        public void setBreadth(double bre)   //设置宽度
        {
            breadth = bre;
        }

        public void steHeight(double hei)   //设置高度
        {
            height = hei;
        }

        public static Box operator+(Box b,Box c)   //重载+运算符来把两个Box对象相加
        {
            Box box = new Box();
            box.length = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height = b.height + c.height;
            return box;
        }

        public static bool operator==(Box lhs,Box rhs)   //lhs 左侧  rhs  右侧
        {
            bool status = false;
            if(lhs.length == rhs.length && lhs.breadth == rhs.breadth && lhs.height == rhs.height)
            {
                status = true;
            }

            return status;       //返回状态
        }

        public static bool operator!=(Box lhs,Box rhs)
        {
            bool status = false;
            if(lhs.length != rhs.length || lhs.breadth != rhs.breadth || lhs.height != rhs.height)
            {
                status = true;
            }

            return status;
        }

        public static bool operator<(Box lhs,Box rhs)
        {
            bool status = false;
            if(lhs.length < rhs.length && lhs.breadth < rhs.breadth && lhs.height < rhs.height)
            {
                status = true;
            }

            return status;
        }

        public static bool operator>(Box lhs,Box rhs)
        {
            bool status = false;
            if(lhs.length > rhs.length && lhs.breadth > rhs.breadth && lhs.height > rhs.height)
            {
                status = true;
            }

            return status;
        }

        public static bool operator<=(Box lhs,Box rhs)
        {
            bool status = false;
            if(lhs.length <= rhs.length && lhs.breadth <= rhs.breadth && lhs.height <= rhs.height)
            {
                status = true;
            }

            return status;
        }

        public static bool operator>=(Box lhs,Box rhs)
        {
            bool status = false;
            if(lhs.length >= rhs.length && lhs.breadth >= rhs.breadth && lhs.height >= rhs.height)
            {
                status = true;
            }

            return status;
        }

        public override string ToString()
        {
            return String.Format("({0},{1},{2})",length,breadth,height);
        }
    }

    class Tester
    {
        static void Main(string[] args)
        {
            Box Box1 = new Box();   //声明Box1,类型为Box
            Box Box2 = new Box();   //声明Box2,类型为Box
            Box Box3 = new Box();   //声明Box3,类型为Box
            Box Box4 = new Box();   //声明Box4,类型为Box
            double volume = 0.0;   //体积

            //Box1详述
            Box1.setLength(6.0);
            Box1.setBreadth(7.0);
            Box1.steHeight(5.0);

            //Box2详述
            Box2.setLength(12.0);
            Box2.setBreadth(13.0);
            Box2.steHeight(10.0);
            
            //使用重载的ToString()显示两个盒子
            Console.WriteLine("Box1:{0}",Box1.ToString());
            Console.WriteLine("Box2:{0}",Box2.ToString());

            //Box1体积
            volume = Box1.getVolume();
            Console.WriteLine("Box1的体积:{0}",volume);

            //Box2体积
            volume = Box2.getVolume();
            Console.WriteLine("Box2的体积:{0}",volume);

            //把两个对象相加
            Box3 = Box1 + Box2;
            Console.WriteLine("Box3:{0}",Box3.ToString());

            //Box3体积
            volume = Box3.getVolume();
            Console.WriteLine("Box3的体积:{0}",volume);

            //比较盒子
            if(Box1 > Box2)
            Console.WriteLine("Box1大于Box2");
            else
            Console.WriteLine("Box1不大于Box2");

            if(Box1 < Box2)
            Console.WriteLine("Box1小于Box2");
            else
            Console.WriteLine("Box1不小于Box2");

            if(Box1 >= Box2)
            Console.WriteLine("Box1大于等于Box2");
            else
            Console.WriteLine("Box1不大于等于Box2");

            if(Box1 <= Box2)
            Console.WriteLine("Box1小于等于Box2");
            else
            Console.WriteLine("Box1不小于等于Box2");

            if(Box1 != Box2)
            Console.WriteLine("Box1不等于Box2");
            else
            Console.WriteLine("Box1等于Box2");

            Box4 = Box3;
            if(Box3 == Box4)
            Console.WriteLine("Box3等于Box4");
            else
            Console.WriteLine("Box3不等于Box4");

            Console.ReadKey();
        }
    }
}