C#运算符重载---逐步地分析与理解

1.什么是运算符重载

定义:(百科定义)就是把已经定义的、有一定功能的操作符进行重新定义,来完成更为细致具体的运算等功能。操作符重载可以将概括性的抽象操作符具体化,便于外部调用而无需知晓内部具体运算过程。

2.为什么需要运算符重载?

在C#中 ,内置的数据类型有:int、double等这些类型是预先在系统内定义好的,可以直接进行数值加减运算。例如:int i, j = 1; int sum=i+j;可以直接得到想要的和sum。

如果要比较一个类(结构)进行对象比较加、减等操作 该如何实现?例如一个类Box,有对象Box1和Box2,那么, 如何实现对象相加Box3 = Box1 + Box2?

这时,我们就要用到具有用户定义类型的运算符。重载运算符是具有特殊名称的功能,关键字operator后跟定义运算符的符号。 类似于任何其他函数定义,重载运算符具有返回类型和参数列表。

3.下面用三个案例,以逐步步进的方式解析一下

 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     }
案例一

C#运算符重载---逐步地分析与理解_第1张图片

 

 

 案例一没有运用运算符重载,也没类或结构相加,可以正常示和;

 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;”,但是没有相应的运算符代码进行支持,所以编译器显示如下提示

C#运算符重载---逐步地分析与理解_第2张图片

 

 

 错误    1    运算符“+”无法应用于“operator_overload.Box”和“operator_overload.Box”类型的操作数

 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         public static Box operator +(Box b, Box c)
55         {
56             Box box = new Box();
57             box.length = b.length + c.length;
58             box.breadth = b.breadth + c.breadth;
59             box.height = b.height + c.height;
60             return box;
61         }
62     }
63 }
案例三

案例三,增加如下运算符重载的支撑代码

   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;
        }

 C#运算符重载---逐步地分析与理解_第3张图片

 

 Box类,Box3 = Box1 + Box2;对应的计算运算式如下:

5878880 = (60+121)*(70+133)*(50+110)

以上案例成功实现了类的相加!

你可能感兴趣的:(C#运算符重载---逐步地分析与理解)