1.什么是运算符重载
定义:(百科定义)就是把已经定义的、有一定功能的操作符进行重新定义,来完成更为细致具体的运算等功能。操作符重载可以将概括性的抽象操作符具体化,便于外部调用而无需知晓内部具体运算过程。
2.为什么需要运算符重载?
在C#中 ,内置的数据类型有:int、double等这些类型是预先在系统内定义好的,可以直接进行数值加减运算。例如:int i, j = 1; int sum=i+j;可以直接得到想要的和sum。
如果要比较一个类(结构)进行对象比较加、减等操作 该如何实现?例如一个类Box,有对象Box1和Box2,那么, 如何实现对象相加Box3 = Box1 + Box2?
这时,我们就要用到具有用户定义类型的运算符。重载运算符是具有特殊名称的功能,关键字operator后跟定义运算符的符号。 类似于任何其他函数定义,重载运算符具有返回类型和参数列表。
3.下面用三个案例,以逐步步进的方式解析一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace operator_overload
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 Box Box1 = new Box();
8 Box Box2 = new Box();
9 Box Box3 = new Box();
10
11 double volume1,volume2 = 0.0;
12
13 Box1.setLength(60.0);
14 Box1.setBreadth(70.0);
15 Box1.setHeight(50.0);
16
17 Box2.setLength(121.0);
18 Box2.setBreadth(133.0);
19 Box2.setHeight(110.0);
20
21 volume1 = Box1.getVolume();
22 Console.WriteLine("Volume of Box1 :{0}",volume1);
23
24 volume2 = Box2.getVolume();
25 Console.WriteLine("Volume of Box1 :{0}", volume2);
26
27
28 }
29 }
30
31 class Box
32 {
33 private double length;
34 private double breadth;
35 private double height;
36
37 public double getVolume()
38 {
39 return length * breadth * height;
40 }
41 public void setLength(double len)
42 {
43 length = len;
44 }
45 public void setBreadth(double bre)
46 {
47 breadth = bre;
48 }
49 public void setHeight(double hei)
50 {
51 height = hei;
52 }
53
54 }
案例一
![](https://img2022.cnblogs.com/blog/1005952/202202/1005952-20220208214715528-1711746059.png)
案例一没有运用运算符重载,也没类或结构相加,可以正常求和;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace operator_overload
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 Box Box1 = new Box();
8 Box Box2 = new Box();
9 Box Box3 = new Box();
10
11 double volume1,volume2 = 0.0;
12
13 Box1.setLength(60.0);
14 Box1.setBreadth(70.0);
15 Box1.setHeight(50.0);
16
17 Box2.setLength(121.0);
18 Box2.setBreadth(133.0);
19 Box2.setHeight(110.0);
20
21 volume1 = Box1.getVolume();
22 Console.WriteLine("Volume of Box1 :{0}",volume1);
23
24 volume2 = Box2.getVolume();
25 Console.WriteLine("Volume of Box1 :{0}", volume2);
26
27 Box3 = Box1 + Box2; //类实例进行相加;
28 }
29 }
30
31 class Box
32 {
33 private double length;
34 private double breadth;
35 private double height;
36
37 public double getVolume()
38 {
39 return length * breadth * height;
40 }
41 public void setLength(double len)
42 {
43 length = len;
44 }
45 public void setBreadth(double bre)
46 {
47 breadth = bre;
48 }
49 public void setHeight(double hei)
50 {
51 height = hei;
52 }
53
54
55 }
56 }
57 }
案例二
案例二,增加了“Box3 = Box1 + Box2;”,但是没有相应的运算符代码进行支持,所以编译器显示如下提示
![](https://img2022.cnblogs.com/blog/1005952/202202/1005952-20220208215516672-1527957789.png)
错误 1 运算符“+”无法应用于“operator_overload.Box”和“operator_overload.Box”类型的操作数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace operator_overload
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Box Box1 = new Box();
13 Box Box2 = new Box();
14 Box Box3 = new Box();
15
16 double volume,volume1,volume2 = 0.0;
17
18 Box1.setLength(60.0);
19 Box1.setBreadth(70.0);
20 Box1.setHeight(50.0);
21
22 Box2.setLength(121.0);
23 Box2.setBreadth(133.0);
24 Box2.setHeight(110.0);
25
26 volume1 = Box1.getVolume();
27 Console.WriteLine("Volume of Box1 :{0}",volume1);
28
29 volume2 = Box2.getVolume();
30 Console.WriteLine("Volume of Box1 :{0}", volume2);
31
32 Box3 = Box1 + Box2;
33
34 volume = Box3.getVolume();
35 Console.WriteLine("Volume of Box3 :{0}", volume);
36 Console.ReadKey();
37 }
38 }
39
40 class Box
41 {
42 private double length;
43 private double breadth;
44 private double height;
45
46 public double getVolume()
47 {
48 return length * breadth * height;
49 }
50 public void setLength(double len)
51 {
52 length = len;
53 }
54 public void setBreadth(double bre)
55 {
56 breadth = bre;
57 }
58 public void setHeight(double hei)
59 {
60 height = hei;
61 }
62
63 public static Box operator +(Box b, Box c)
64 {
65 Box box = new Box();
66 box.length = b.length + c.length;
67 box.breadth = b.breadth + c.breadth;
68 box.height = b.height + c.height;
69 return box;
70 }
71 }
72 }
案例三
案例三,增加如下运算符重载的支撑代码
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;
}
![](https://img2022.cnblogs.com/blog/1005952/202202/1005952-20220208220317062-835503957.png)
Box类,Box3 = Box1 + Box2;对应的计算运算式如下:
5878880 = (60+121)*(70+133)*(50+110)
以上案例成功实现了类的相加!
|