当前位置: 首页 > 文档资料 > C# 中文指南 >

C# 基础 - 运算符重载

优质
小牛编辑
148浏览
2023-12-01

你可以重新定义或重载大部分 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. namespace OperatorOvlApplication
  3. {
  4. class Box
  5. {
  6. private double length; // box 的长度
  7. private double breadth; // box 的宽度
  8. private double height; // box 的高度
  9. public double getVolume()
  10. {
  11. return length * breadth * height;
  12. }
  13. public void setLength( double len )
  14. {
  15. length = len;
  16. }
  17. public void setBreadth( double bre )
  18. {
  19. breadth = bre;
  20. }
  21. public void setHeight( double hei )
  22. {
  23. height = hei;
  24. }
  25. // 重载 + operator 来增加两个 Box 对象
  26. public static Box operator+ (Box b, Box c)
  27. {
  28. Box box = new Box();
  29. box.length = b.length + c.length;
  30. box.breadth = b.breadth + c.breadth;
  31. box.height = b.height + c.height;
  32. return box;
  33. }
  34. }
  35. class Tester
  36. {
  37. static void Main(string[] args)
  38. {
  39. Box Box1 = new Box(); // 声明 box1 为 box 类型
  40. Box Box2 = new Box(); // 声明 box2 为 box 类型
  41. Box Box3 = new Box(); // 声明 box3 为 box 类型
  42. double volume = 0.0; // 在这里存放 box 的体积
  43. // box 1 详细数据
  44. Box1.setLength(6.0);
  45. Box1.setBreadth(7.0);
  46. Box1.setHeight(5.0);
  47. // box 2 详细数据
  48. Box2.setLength(12.0);
  49. Box2.setBreadth(13.0);
  50. Box2.setHeight(10.0);
  51. // box 1 的体积
  52. volume = Box1.getVolume();
  53. Console.WriteLine("Volume of Box1 : {0}", volume);
  54. // box 2 的体积
  55. volume = Box2.getVolume();
  56. Console.WriteLine("Volume of Box2 : {0}", volume);
  57. // 将两个对象相加如下:
  58. Box3 = Box1 + Box2;
  59. // box 3 的体积
  60. volume = Box3.getVolume();
  61. Console.WriteLine("Volume of Box3 : {0}", volume);
  62. Console.ReadKey();
  63. }
  64. }
  65. }

编译执行上述代码,得到如下结果:

  1. Volume of Box1 : 210
  2. Volume of Box2 : 1560
  3. Volume of Box3 : 5400

可重载与不可重载的运算符

下表列出了 C# 中运算符的可重载能力:

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

示例

针对上述讨论,让我们扩展上面的例子,重载更多的操作符:

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

编译执行上述代码,得到如下结果:

  1. Box 1: (6, 7, 5)
  2. Box 2: (12, 13, 10)
  3. Volume of Box1 : 210
  4. Volume of Box2 : 1560
  5. Box 3: (18, 20, 15)
  6. Volume of Box3 : 5400
  7. Box1 is not greater than Box2
  8. Box1 is less than Box2
  9. Box1 is not greater or equal to Box2
  10. Box1 is less or equal to Box2
  11. Box1 is not equal to Box2
  12. Box3 is equal to Box4