C#运算符重载

运算符重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/*
 运算符重载
     */
namespace test14
{
    class Box
    {
        private double length, breadth, heigth;
        
        public double Length { get; set; }
        public double Breadth { get; set; }
        public double Heigth { get; set; }

        public double getVolume()
        {
            return Length * Breadth * Heigth;
        }

        //重载+
        public static Box operator + (Box b, Box c)
        {
            Box box = new Box();

            box.Length = b.Length + c.Length;
            box.Breadth = b.Breadth + c.Breadth;
            box.Heigth = b.Heigth + c.Heigth;

            return box;
        }

        //重载==
        public static bool operator == (Box lhs, Box rhs)
        {
            bool status = false;

            if(lhs.Length == rhs.Length && lhs.Breadth == rhs.Breadth && lhs.Heigth == rhs.Heigth)
            {
                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.Heigth != rhs.Heigth)
            {
                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.Heigth < rhs.Heigth)
            {
                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.Heigth > rhs.Heigth)
            {
                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.Heigth <= rhs.Heigth)
            {
                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.Heigth >= rhs.Heigth)
            {
                status = true;
            }

            return status;
            
        }
        public override string ToString()
        {
            return String.Format("({0}, {1}, {2})", Length, Breadth, Heigth);
        }



    }




    class Program
    {
        static void Main(string[] args)
        {
            Box box1 = new Box();
            Box box2 = new Box();
            Box box3 = new Box();
            Box box4 = new Box();
            double volume = 0.0;

            //初始化box1
            box1.Length = 6.0;
            box1.Breadth = 7.0;
            box1.Heigth = 5.0;

            //初始化box2
            box2.Length = 12.0;
            box2.Breadth = 13.0;
            box2.Heigth = 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);

            //comparing the boxes

            if(box1 > box2)
            {
                Console.WriteLine("box1 > box2");
            }
            else
            {
                Console.WriteLine("box1 not> box2");
            }

            if (box1 < box2)
            {
                Console.WriteLine("box1 < box2");
            }
            else
            {
                Console.WriteLine("box1 not< box2");
            }

            if (box1 >= box2)
            {
                Console.WriteLine("box1 >= box2");
            }
            else
            {
                Console.WriteLine("box1 not>= box2");
            }

            if (box1 <= box2)
            {
                Console.WriteLine("box1 <= box2");
            }
            else
            {
                Console.WriteLine("box1 not<= 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();


        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

困了就喝白茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值