课程表

C# 基础教程

C# 高级教程

C# 工具/手册

工具箱
速查手册

C# 运算符重载

当前位置:免费教程 » 程序设计 » C#

您可以重定义或重载 C# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。

例如,请看下面的函数:

  1. public static Box operator+ (Box b, Box c)
  2. {
  3. Box box = new Box();
  4. box.length = b.length + c.length;
  5. box.breadth = b.breadth + c.breadth;
  6. box.height = b.height + c.height;
  7. return box;
  8. }

上面的函数为用户自定义的类 Box 实现了加法运算符(+)。它把两个 Box 对象的属性相加,并返回相加后的 Box 对象。

运算符重载的实现

下面的程序演示了完整的实现:

  1. using System;
  2.  
  3. namespace OperatorOvlApplication
  4. {
  5. class Box
  6. {
  7. private double length; // 长度
  8. private double breadth; // 宽度
  9. private double height; // 高度
  10.  
  11. public double getVolume()
  12. {
  13. return length * breadth * height;
  14. }
  15. public void setLength( double len )
  16. {
  17. length = len;
  18. }
  19.  
  20. public void setBreadth( double bre )
  21. {
  22. breadth = bre;
  23. }
  24.  
  25. public void setHeight( double hei )
  26. {
  27. height = hei;
  28. }
  29. // 重载 + 运算符来把两个 Box 对象相加
  30. public static Box operator+ (Box b, Box c)
  31. {
  32. Box box = new Box();
  33. box.length = b.length + c.length;
  34. box.breadth = b.breadth + c.breadth;
  35. box.height = b.height + c.height;
  36. return box;
  37. }
  38.  
  39. }
  40.  
  41. class Tester
  42. {
  43. static void Main(string[] args)
  44. {
  45. Box Box1 = new Box(); // 声明 Box1,类型为 Box
  46. Box Box2 = new Box(); // 声明 Box2,类型为 Box
  47. Box Box3 = new Box(); // 声明 Box3,类型为 Box
  48. double volume = 0.0; // 体积
  49.  
  50. // Box1 详述
  51. Box1.setLength(6.0);
  52. Box1.setBreadth(7.0);
  53. Box1.setHeight(5.0);
  54.  
  55. // Box2 详述
  56. Box2.setLength(12.0);
  57. Box2.setBreadth(13.0);
  58. Box2.setHeight(10.0);
  59.  
  60. // Box1 的体积
  61. volume = Box1.getVolume();
  62. Console.WriteLine("Box1 的体积: {0}", volume);
  63.  
  64. // Box2 的体积
  65. volume = Box2.getVolume();
  66. Console.WriteLine("Box2 的体积: {0}", volume);
  67.  
  68. // 把两个对象相加
  69. Box3 = Box1 + Box2;
  70.  
  71. // Box3 的体积
  72. volume = Box3.getVolume();
  73. Console.WriteLine("Box3 的体积: {0}", volume);
  74. Console.ReadKey();
  75. }
  76. }
  77. }

我来试一下

当上面的代码被编译和执行时,它会产生下列结果:

Box1 的体积: 210
Box2 的体积: 1560
Box3 的体积: 5400

可重载和不可重载运算符

下表描述了 C# 中运算符重载的能力:

运算符描述
+, -, !, ~, ++, --这些一元运算符只有一个操作数,且可以被重载。
+, -, *, /, %这些二元运算符带有两个操作数,且可以被重载。
==, !=, <, >, <=, >=这些比较运算符可以被重载。
&&, ||这些条件逻辑运算符不能被直接重载。
+=, -=, *=, /=, %=这些赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能被重载。

实例

针对上述讨论,让我们扩展上面的实例,重载更多的运算符:

  1. using System;
  2.  
  3. namespace OperatorOvlApplication
  4. {
  5. class Box
  6. {
  7. private double length; // 长度
  8. private double breadth; // 宽度
  9. private double height; // 高度
  10. public double getVolume()
  11. {
  12. return length * breadth * height;
  13. }
  14. public void setLength( double len )
  15. {
  16. length = len;
  17. }
  18.  
  19. public void setBreadth( double bre )
  20. {
  21. breadth = bre;
  22. }
  23.  
  24. public void setHeight( double hei )
  25. {
  26. height = hei;
  27. }
  28. // 重载 + 运算符来把两个 Box 对象相加
  29. public static Box operator+ (Box b, Box c)
  30. {
  31. Box box = new Box();
  32. box.length = b.length + c.length;
  33. box.breadth = b.breadth + c.breadth;
  34. box.height = b.height + c.height;
  35. return box;
  36. }
  37. public static bool operator == (Box lhs, Box rhs)
  38. {
  39. bool status = false;
  40. if (lhs.length == rhs.length && lhs.height == rhs.height
  41. && lhs.breadth == rhs.breadth)
  42. {
  43. status = true;
  44. }
  45. return status;
  46. }
  47. public static bool operator !=(Box lhs, Box rhs)
  48. {
  49. bool status = false;
  50. if (lhs.length != rhs.length || lhs.height != rhs.height
  51. || lhs.breadth != rhs.breadth)
  52. {
  53. status = true;
  54. }
  55. return status;
  56. }
  57. public static bool operator <(Box lhs, Box rhs)
  58. {
  59. bool status = false;
  60. if (lhs.length < rhs.length && lhs.height
  61. < rhs.height && lhs.breadth < rhs.breadth)
  62. {
  63. status = true;
  64. }
  65. return status;
  66. }
  67.  
  68. public static bool operator >(Box lhs, Box rhs)
  69. {
  70. bool status = false;
  71. if (lhs.length > rhs.length && lhs.height
  72. > rhs.height && lhs.breadth > rhs.breadth)
  73. {
  74. status = true;
  75. }
  76. return status;
  77. }
  78.  
  79. public static bool operator <=(Box lhs, Box rhs)
  80. {
  81. bool status = false;
  82. if (lhs.length <= rhs.length && lhs.height
  83. <= rhs.height && lhs.breadth <= rhs.breadth)
  84. {
  85. status = true;
  86. }
  87. return status;
  88. }
  89.  
  90. public static bool operator >=(Box lhs, Box rhs)
  91. {
  92. bool status = false;
  93. if (lhs.length >= rhs.length && lhs.height
  94. >= rhs.height && lhs.breadth >= rhs.breadth)
  95. {
  96. status = true;
  97. }
  98. return status;
  99. }
  100. public override string ToString()
  101. {
  102. return String.Format("({0}, {1}, {2})", length, breadth, height);
  103. }
  104. }
  105. class Tester
  106. {
  107. static void Main(string[] args)
  108. {
  109. Box Box1 = new Box(); // 声明 Box1,类型为 Box
  110. Box Box2 = new Box(); // 声明 Box2,类型为 Box
  111. Box Box3 = new Box(); // 声明 Box3,类型为 Box
  112. Box Box4 = new Box();
  113. double volume = 0.0; // 体积
  114.  
  115. // Box1 详述
  116. Box1.setLength(6.0);
  117. Box1.setBreadth(7.0);
  118. Box1.setHeight(5.0);
  119.  
  120. // Box2 详述
  121. Box2.setLength(12.0);
  122. Box2.setBreadth(13.0);
  123. Box2.setHeight(10.0);
  124.  
  125. // 使用重载的 ToString() 显示两个盒子
  126. Console.WriteLine("Box1: {0}", Box1.ToString());
  127. Console.WriteLine("Box2: {0}", Box2.ToString());
  128. // Box1 的体积
  129. volume = Box1.getVolume();
  130. Console.WriteLine("Box1 的体积: {0}", volume);
  131.  
  132. // Box2 的体积
  133. volume = Box2.getVolume();
  134. Console.WriteLine("Box2 的体积: {0}", volume);
  135.  
  136. // 把两个对象相加
  137. Box3 = Box1 + Box2;
  138. Console.WriteLine("Box3: {0}", Box3.ToString());
  139. // Box3 的体积
  140. volume = Box3.getVolume();
  141. Console.WriteLine("Box3 的体积: {0}", volume);
  142.  
  143. //comparing the boxes
  144. if (Box1 > Box2)
  145. Console.WriteLine("Box1 大于 Box2");
  146. else
  147. Console.WriteLine("Box1 不大于 Box2");
  148. if (Box1 < Box2)
  149. Console.WriteLine("Box1 小于 Box2");
  150. else
  151. Console.WriteLine("Box1 不小于 Box2");
  152. if (Box1 >= Box2)
  153. Console.WriteLine("Box1 大于等于 Box2");
  154. else
  155. Console.WriteLine("Box1 不大于等于 Box2");
  156. if (Box1 <= Box2)
  157. Console.WriteLine("Box1 小于等于 Box2");
  158. else
  159. Console.WriteLine("Box1 不小于等于 Box2");
  160. if (Box1 != Box2)
  161. Console.WriteLine("Box1 不等于 Box2");
  162. else
  163. Console.WriteLine("Box1 等于 Box2");
  164. Box4 = Box3;
  165. if (Box3 == Box4)
  166. Console.WriteLine("Box3 等于 Box4");
  167. else
  168. Console.WriteLine("Box3 不等于 Box4");
  169.  
  170. Console.ReadKey();
  171. }
  172. }
  173. }

我来试一下

当上面的代码被编译和执行时,它会产生下列结果:

Box1: (6, 7, 5)
Box2: (12, 13, 10)
Box1 的体积: 210
Box2 的体积: 1560
Box3: (18, 20, 15)
Box3 的体积: 5400
Box1 不大于 Box2
Box1 小于 Box2
Box1 不大于等于 Box2
Box1 小于等于 Box2
Box1 不等于 Box2
Box3 等于 Box4
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号