一个自定义类型的运算符重载的例子
这里只重载了“+”“-”“==”“!=”运算符。
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleDemo.Classes
7 {
8 public class Vector
9 {
10 public double X { get; set; }
11
12 public double Y { get; set; }
13
14 public double Z { get; set; }
15
16 public Vector(double x, double y, double z)
17 {
18 this.X = x;
19 this.Y = y;
20 this.Z = z;
21 }
22
23 public Vector(Vector vector)
24 {
25 this.X = vector.X;
26 this.Y = vector.Y;
27 this.Z = vector.Z;
28 }
29
30 public override string ToString()
31 {
32 return "X = " + this.X + ", Y = " + this.Y + ", Z = " + this.Z;
33 }
34
35 /// <summary>
36 /// 重载 ConsoleDemo.Classes.Vector 类型的“+”运算符。
37 /// </summary>
38 /// <param name="lhs">运算符左侧实例</param>
39 /// <param name="rhs">运算符右侧实例</param>
40 /// <returns></returns>
41 public static Vector operator +(Vector lhs, Vector rhs)
42 {
43 Vector result = new Vector(lhs);
44 result.X += rhs.X;
45 result.Y += rhs.Y;
46 result.Z += rhs.Z;
47 return result;
48 }
49
50 /// <summary>
51 /// 重载 ConsoleDemo.Classes.Vector 类型的“-”运算符。
52 /// </summary>
53 /// <param name="lhs">运算符左侧实例</param>
54 /// <param name="rhs">运算符右侧实例</param>
55 /// <returns></returns>
56 public static Vector operator -(Vector lhs, Vector rhs)
57 {
58 Vector result = new Vector(lhs);
59 result.X -= rhs.X;
60 result.Y -= rhs.Y;
61 result.Z -= rhs.Z;
62 return result;
63 }
64
65 // "*" "/" "%" etc.
66
67 /// <summary>
68 /// 重载 ConsoleDemo.Classes.Vector 类型的“==”运算符
69 /// </summary>
70 /// <param name="lhs">运算符左侧实例</param>
71 /// <param name="rhs">运算符右侧实例</param>
72 /// <returns></returns>
73 public static bool operator ==(Vector lhs, Vector rhs)
74 {
75 if (lhs.X == rhs.X && lhs.Y == rhs.Y && lhs.Z == rhs.Z)
76 {
77 return true;
78 }
79
80 return false;
81 }
82
83 /// <summary>
84 /// 重载 ConsoleDemo.Classes.Vector 类型的“!=”运算符
85 /// </summary>
86 /// <param name="lhs">运算符左侧实例</param>
87 /// <param name="rhs">运算符右侧实例</param>
88 /// <returns></returns>
89 public static bool operator !=(Vector lhs, Vector rhs)
90 {
91 return !(lhs == rhs);
92 }
93
94 /// <summary>
95 /// 重写 Object.Equals() 方法,判断 ConsoleDemo.Classes.Vector 类型是否相等
96 /// </summary>
97 /// <param name="obj"></param>
98 /// <returns></returns>
99 public override bool Equals(object obj)
100 {
101 Vector tempVector = obj as Vector;
102
103 if (tempVector == null)
104 {
105 return false;
106 }
107
108 return this == tempVector;
109 }
110
111 /// <summary>
112 /// 重写 Object.GetHashCode() 方法,与 Object.GetHashCode() 方法等效
113 /// </summary>
114 /// <returns></returns>
115 public override int GetHashCode()
116 {
117 return base.GetHashCode();
118 }
119
120 }
121 }
122
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleDemo.Classes
7 {
8 public class Vector
9 {
10 public double X { get; set; }
11
12 public double Y { get; set; }
13
14 public double Z { get; set; }
15
16 public Vector(double x, double y, double z)
17 {
18 this.X = x;
19 this.Y = y;
20 this.Z = z;
21 }
22
23 public Vector(Vector vector)
24 {
25 this.X = vector.X;
26 this.Y = vector.Y;
27 this.Z = vector.Z;
28 }
29
30 public override string ToString()
31 {
32 return "X = " + this.X + ", Y = " + this.Y + ", Z = " + this.Z;
33 }
34
35 /// <summary>
36 /// 重载 ConsoleDemo.Classes.Vector 类型的“+”运算符。
37 /// </summary>
38 /// <param name="lhs">运算符左侧实例</param>
39 /// <param name="rhs">运算符右侧实例</param>
40 /// <returns></returns>
41 public static Vector operator +(Vector lhs, Vector rhs)
42 {
43 Vector result = new Vector(lhs);
44 result.X += rhs.X;
45 result.Y += rhs.Y;
46 result.Z += rhs.Z;
47 return result;
48 }
49
50 /// <summary>
51 /// 重载 ConsoleDemo.Classes.Vector 类型的“-”运算符。
52 /// </summary>
53 /// <param name="lhs">运算符左侧实例</param>
54 /// <param name="rhs">运算符右侧实例</param>
55 /// <returns></returns>
56 public static Vector operator -(Vector lhs, Vector rhs)
57 {
58 Vector result = new Vector(lhs);
59 result.X -= rhs.X;
60 result.Y -= rhs.Y;
61 result.Z -= rhs.Z;
62 return result;
63 }
64
65 // "*" "/" "%" etc.
66
67 /// <summary>
68 /// 重载 ConsoleDemo.Classes.Vector 类型的“==”运算符
69 /// </summary>
70 /// <param name="lhs">运算符左侧实例</param>
71 /// <param name="rhs">运算符右侧实例</param>
72 /// <returns></returns>
73 public static bool operator ==(Vector lhs, Vector rhs)
74 {
75 if (lhs.X == rhs.X && lhs.Y == rhs.Y && lhs.Z == rhs.Z)
76 {
77 return true;
78 }
79
80 return false;
81 }
82
83 /// <summary>
84 /// 重载 ConsoleDemo.Classes.Vector 类型的“!=”运算符
85 /// </summary>
86 /// <param name="lhs">运算符左侧实例</param>
87 /// <param name="rhs">运算符右侧实例</param>
88 /// <returns></returns>
89 public static bool operator !=(Vector lhs, Vector rhs)
90 {
91 return !(lhs == rhs);
92 }
93
94 /// <summary>
95 /// 重写 Object.Equals() 方法,判断 ConsoleDemo.Classes.Vector 类型是否相等
96 /// </summary>
97 /// <param name="obj"></param>
98 /// <returns></returns>
99 public override bool Equals(object obj)
100 {
101 Vector tempVector = obj as Vector;
102
103 if (tempVector == null)
104 {
105 return false;
106 }
107
108 return this == tempVector;
109 }
110
111 /// <summary>
112 /// 重写 Object.GetHashCode() 方法,与 Object.GetHashCode() 方法等效
113 /// </summary>
114 /// <returns></returns>
115 public override int GetHashCode()
116 {
117 return base.GetHashCode();
118 }
119
120 }
121 }
122
需要注意的是在重写比较运算符时:运算符总是要成对出现的;并且在重写“==”时也要重写 Equals() 和 GetHashCode() 方法。