C#運算子過載---逐步地分析與理解

chenlight發表於2022-02-08

1.什麼是運算子過載

定義:(百科定義)就是把已經定義的、有一定功能的操作符進行重新定義,來完成更為細緻具體的運算等功能。操作符過載可以將概括性的抽象操作符具體化,便於外部呼叫而無需知曉內部具體運算過程。

2.為什麼需要運算子過載?

在C#中 ,內建的資料型別有:int、double等這些型別是預先在系統內定義好的,可以直接進行數值加減運算。例如:int i, j = 1; int sum=i+j;可以直接得到想要的和sum。

如果要比較一個類(結構)進行物件比較加、減等操作 該如何實現?例如一個類Box,有物件Box1和Box2,那麼, 如何實現物件相加Box3 = Box1 + Box2?

這時,我們就要用到具有使用者定義型別的運算子。過載運算子是具有特殊名稱的功能,關鍵字operator後跟定義運算子的符號。 類似於任何其他函式定義,過載運算子具有返回型別和引數列表。

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

C#運算子過載---逐步地分析與理解
 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 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;”,但是沒有相應的運算子程式碼進行支援,所以編譯器顯示如下提示

 

 

 錯誤    1    運算子“+”無法應用於“operator_overload.Box”和“operator_overload.Box”型別的運算元

C#運算子過載---逐步地分析與理解
 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;
        }

 

 

 Box類,Box3 = Box1 + Box2;對應的計算運算式如下:

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

以上案例成功實現了類的相加!

相關文章