C#运算符重载---逐步地分析与理解
Posted chenlight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#运算符重载---逐步地分析与理解相关的知识,希望对你有一定的参考价值。
1.什么是运算符重载
定义:(百科定义)就是把已经定义的、有一定功能的操作符进行重新定义,来完成更为细致具体的运算等功能。操作符重载可以将概括性的抽象操作符具体化,便于外部调用而无需知晓内部具体运算过程。
2.为什么需要运算符重载?
在C#中 ,内置的数据类型有:int、double等这些类型是预先在系统内定义好的,可以直接进行数值加减运算。例如:int i, j = 1; int sum=i+j;可以直接得到想要的和sum。
如果要比较一个类(结构)进行对象比较加、减等操作 该如何实现?例如一个类Box,有对象Box1和Box2,那么, 如何实现对象相加Box3 = Box1 + Box2?
这时,我们就要用到具有用户定义类型的运算符。重载运算符是具有特殊名称的功能,关键字operator
后跟定义运算符的符号。 类似于任何其他函数定义,重载运算符具有返回类型和参数列表。
3.下面用三个案例,以逐步步进的方式解析一下
namespace operator_overload class Program static void Main(string[] args) Box Box1 = new Box(); Box Box2 = new Box(); Box Box3 = new Box(); double volume1,volume2 = 0.0; Box1.setLength(60.0); Box1.setBreadth(70.0); Box1.setHeight(50.0); Box2.setLength(121.0); Box2.setBreadth(133.0); Box2.setHeight(110.0); volume1 = Box1.getVolume(); Console.WriteLine("Volume of Box1 :0",volume1); volume2 = Box2.getVolume(); Console.WriteLine("Volume of Box1 :0", volume2); 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;
案例一没有运用运算符重载,也没类或结构相加,可以正常求和;
namespace operator_overload class Program static void Main(string[] args) Box Box1 = new Box(); Box Box2 = new Box(); Box Box3 = new Box(); double volume1,volume2 = 0.0; Box1.setLength(60.0); Box1.setBreadth(70.0); Box1.setHeight(50.0); Box2.setLength(121.0); Box2.setBreadth(133.0); Box2.setHeight(110.0); volume1 = Box1.getVolume(); Console.WriteLine("Volume of Box1 :0",volume1); volume2 = Box2.getVolume(); Console.WriteLine("Volume of Box1 :0", volume2); Box3 = Box1 + Box2; //类实例进行相加; 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;
案例二,增加了“Box3 = Box1 + Box2;”,但是没有相应的运算符代码进行支持,所以编译器显示如下提示
错误 1 运算符“+”无法应用于“operator_overload.Box”和“operator_overload.Box”类型的操作数
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace operator_overload class Program static void Main(string[] args) Box Box1 = new Box(); Box Box2 = new Box(); Box Box3 = new Box(); double volume,volume1,volume2 = 0.0; Box1.setLength(60.0); Box1.setBreadth(70.0); Box1.setHeight(50.0); Box2.setLength(121.0); Box2.setBreadth(133.0); Box2.setHeight(110.0); volume1 = Box1.getVolume(); Console.WriteLine("Volume of Box1 :0",volume1); volume2 = Box2.getVolume(); Console.WriteLine("Volume of Box1 :0", volume2); Box3 = Box1 + Box2; volume = Box3.getVolume(); Console.WriteLine("Volume of Box3 :0", volume); Console.ReadKey(); 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;
Box类,Box3 = Box1 + Box2;对应的计算运算式如下:
5878880 = (60+121)*(70+133)*(50+110)
以上是关于C#运算符重载---逐步地分析与理解的主要内容,如果未能解决你的问题,请参考以下文章