C#运算符重载

来源:互联网 发布:系统动力学软件stella 编辑:程序博客网 时间:2024/05/18 03:37
重载语句:
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