练习重载, 举了一个例子, 比如复数的运算, 用工厂模式实现加减乘除
练习工厂模式之前, 先练习一下重载, 实现了复数plu1和plu2的加法运算
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Microsoft.VisualBasic; 6 7 namespace CSharp_Test 8 { 9 struct plural // 定义复数的结构体 10 { 11 public double x, y; 12 public plural( double x, double y) // 构造复数方法1 13 { 14 this .x = x; 15 this .y = y; 16 } 17 public plural(plural plu) // 构造复数方法2 18 { 19 x = plu.x; 20 y = plu.y; 21 } 22 public override string ToString() // 重载ToString()方法 23 { 24 string a = (y > 0 ) ? (x + " + " + y + " i " ):(x + "" + y + " i " ); 25 return a; 26 } 27 public static plural operator + (plural lhs, plural rhs) // 重载+号运算 28 { 29 plural result = new plural(lhs); 30 result.x += rhs.x; 31 result.y += rhs.y; 32 return result; 33 } 34 35 static void Main() 36 { 37 plural plu1, plu2, plu3; // 定义了三个复数 38 plu1 = new plural( 1 , 1 ); // 复数1赋值 39 plu2 = new plural( 2 , - 3 ); // 复数2赋值 40 plu3 = plu1 + plu2; // 复数3 = 复数1 + 复数2 41 Console.WriteLine( " plu1 = " + plu1.ToString()); 42 Console.WriteLine( " plu2 = " + plu2.ToString()); 43 Console.WriteLine( " plu3 = " + plu3.ToString()); 44 } 45 } 46 }
在C#的三目运算符中有一个注意的地方:
24 string a = (y > 0 ) ? (x + " + " + y + " i " ):(x + "" + y + " i " );
在C#中三目运算符不能单独构成语句,这点是和C语言不同的,所以要和三目运算符构成赋值语句, 如果这样写就是错误的:
22 public override string ToString() // 重载ToString()方法 23 { 24 (y > 0 ) ? ( return x + " + " + y + " i " ):( return x + "" + y + " i " ); //错误 25 }
运行结果是:
---------------------------------------------------------------------------------------------------
下面弄了个加减乘除运算的练习, 使用工厂模式(问题随之而来)复数的运算法则如下:
加:(a+bi)+(c+di)=(a+c)+(b+d)i
减:(a+bi)-(c+di)=(a-c)+(b-d)i
乘:(a+bi)•(c+di)=(ac-bd)+(bc+ad)i
除:(a+bi)÷(c+di)=[(ac+bd) / (c^2+d^2)]+[(bc-ad) / (c^2+d^2)] i) //c与d不同时为零且c+di不等于0
使用工厂模式进行的加减乘除运算代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Microsoft.VisualBasic; 6 7 namespace CSharp_Test 8 { 9 public struct plural // 定义复数的结构体 10 { 11 public double x, y; 12 public plural( double x, double y) // 构造复数方法1 13 { 14 this .x = x; 15 this .y = y; 16 } 17 public plural(plural plu) // 构造复数方法2 18 { 19 x = plu.x; 20 y = plu.y; 21 } 22 public override string ToString() // 重载ToString()方法 23 { 24 string result = (y >= 0 ) ? (x + " + " + y + " i " ):(x + "" + y + " i " ); 25 return result; 26 } 27 public static plural operator + (plural lhs, plural rhs) // 重载+号运算 就在这边出现疑问了(不是错误) 28 { 29 plural result = new plural(lhs); 30 result.x += rhs.x; 31 result.y += rhs.y; 32 return result; 33 } 34 public static plural operator - (plural lhs, plural rhs) // 重载-号运算 35 { 36 plural result = new plural(lhs); 37 result.x -= rhs.x; 38 result.y -= rhs.y; 39 return result; 40 } 41 public static plural operator * (plural lhs, plural rhs) // 重载*号运算 42 { 43 plural result = new plural(lhs); 44 result.x = lhs.x * rhs.x - lhs.x * rhs.y; 45 result.y = lhs.y * rhs.x + lhs.x * rhs.y; 46 return result; 47 } 48 49 public static plural operator / (plural lhs, plural rhs) // 重载/号运算 50 { 51 plural result = new plural(lhs); 52 result.x = (lhs.x * rhs.x + lhs.y * rhs.y) / (rhs.x * rhs.x + rhs.y * rhs.y); 53 result.y = (lhs.y * rhs.x - lhs.x * rhs.y) / (rhs.x * rhs.x + rhs.y * rhs.y); 54 return result; 55 } 56 } // 到此结构体定义结束 57 58 public class Operation // 在这个类中操作,操作方法进行虚拟 59 { 60 private plural lhs = new plural( 0 , 0 ); 61 private plural rhs = new plural( 0 , 0 ); 62 public plural A 63 { 64 get { return lhs;} 65 set { this .lhs = value;} 66 } 67 public plural B 68 { 69 get { return rhs;} 70 set { this .rhs = value;} 71 } 72 public virtual plural GetResult() // 操作的方法 73 { 74 plural result = new plural( 0 , 0 ); 75 return result; 76 } 77 } 78 public class OperationAdd : Operation // operation类中有个加法操作 79 { // 为什么上面红色标记的部分不能写在这边????????????? 80 public override plural GetResult() // 重载操作方法,加法是如何实现 81 { 82 plural result; 83 result = A + B; 84 return result; 85 } 86 } 87 public class OperationSub : Operation // operation类中有个减法操作 88 { 89 public override plural GetResult() // 重载操作方法,减法是如何实现 90 { 91 plural result; 92 result = A - B; 93 return result; 94 } 95 } 96 public class OperationMul : Operation // operation类中有个乘法操作 97 { 98 public override plural GetResult() // 重载操作方法,乘法是如何实现 99 { 100 plural result; 101 result = A * B; 102 return result; 103 } 104 } 105 public class OpetationDiv : Operation // operation类中有个除法操作 106 { 107 public override plural GetResult() // 重载操作方法,除法是如何实现 108 { 109 if ((B.x == 0 ) && (B.y == 0 )) // c与d不同时为零且c+di不等于0 110 throw new Exception( " The second plural error! " ); 111 plural result; 112 result = A / B; 113 return result; 114 } 115 } 116 117 118 public class OperationFactory // 简单的操作工厂,判断进行何种运算 119 { 120 public static Operation createOperate( string operate) 121 { 122 Operation oper = null ; 123 switch (operate) 124 { 125 case " + " : 126 oper = new OperationAdd(); 127 break ; 128 case " - " : 129 oper = new OperationSub(); 130 break ; 131 case " * " : 132 oper = new OperationMul(); 133 break ; 134 case " / " : 135 oper = new OpetationDiv(); 136 break ; 137 } 138 return oper; 139 } 140 } 141 142 class main 143 { 144 static void Main() 145 { 146 plural C; 147 Operation oper = new Operation(); 148 oper = OperationFactory.createOperate( " + " ); // 加法 149 oper.A = new plural( 1 , 1 ); 150 oper.B = new plural( 2 , - 4 ); 151 Console.WriteLine( " A = " + oper.A.ToString()); 152 Console.WriteLine( " B = " + oper.B.ToString()); 153 C = oper.GetResult(); 154 Console.WriteLine( " A+B= " + C.ToString()); 155 156 oper = OperationFactory.createOperate( " - " ); // 减法 157 oper.A = new plural( 1 , 1 ); 158 oper.B = new plural( 2 , - 4 ); 159 C = oper.GetResult(); 160 Console.WriteLine( " A-B= " + C.ToString()); 161 162 oper = OperationFactory.createOperate( " * " ); // 乘法 163 oper.A = new plural( 1 , 1 ); 164 oper.B = new plural( 2 , - 4 ); 165 C = oper.GetResult(); 166 Console.WriteLine( " A*B= " + C.ToString()); 167 168 oper = OperationFactory.createOperate( " / " ); // 除法 169 oper.A = new plural( 1 , 1 ); 170 oper.B = new plural( 2 , - 4 ); 171 C = oper.GetResult(); 172 Console.WriteLine( " A/B= " + C.ToString()); 173 Console.ReadLine(); 174 } 175 } 176 }
运行结果:
----------------------------------------------------------------------------------------------------
显然在结构体内把所有可能的运算方式都重载一下, 势必提高耦合度! 这样子的工厂模式写法就有些半调子了
但讨论之后发现: 重载结构体的运算符一定要在结构体内完成...