C#运算符重载

重载语句:
public static <数据类型> operator<运算符> (数据类型 a,数据类型 b)
例子
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.height = b.height - c.height;
            return box;
        }


运算符重载规则:
可以被重载的类型:
+,-,~,++,--   一元运算符带有一个操作数,可以被重载
+,-,*,/,%      二元运算符带有两个操作数,可以被重载
==,!=,<,>,<=,>=  比较运算符可以被重载
不可被重载的类型: 
&&,||             条件逻辑运算符不能被直接重载
+=,-=,*=,/=,%=    赋值运算符不能被重载
=, . ,?: ,-> ,new ,is ,sizeof, typeof 



重载输出的ToString()函数,对于输出数据结构中的内容来说,很关键
For Example:
public override string ToString()
        {
            return String.Format("{0}, {1}, {2}", length, breadth, height);

        }


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

namespace OperatorOvlApplication
{
    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 setHeight(double hei)
        {
            height = hei;
        }
        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.height = b.height + c.height;
            return box;
        }
        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.height = b.height - c.height;
            return box;
        }
        public override string ToString()
        {
            return String.Format("{0}, {1}, {2}", length, breadth, height);
        }
    }
    class Tester
    {
        static void Main(string[] args)
        {
            Box box1 = new Box();
            Box box2 = new Box();
            Box box3 = new Box();
            double volume = 0.0;

            // Box1 详述
            box1.setLength(6.0);
            box1.setBreadth(7.0);
            box1.setHeight(5.0);
            
            // Box2 详述
            box2.setLength(12.0);
            box2.setBreadth(13.0);
            box2.setHeight(10.0);

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

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

            box3 = box2 + box1;
            Console.WriteLine("Box3: {0}",box3.ToString());
            volume = box3.getVolume();
            Console.WriteLine("Box3的体积:{0}", volume);
            Console.ReadLine();
        }
    }
}

程序运行结果如下,(供参考)

加载失败。。。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值