C#入门(三)—面向对象思想
1、面向对象三大特性:封装、继承、多态;四大好处:可维护、可复用、可扩展、灵活性好。
2、业务的封装:高内聚、低耦合。
3、赋值!=复用。
4、简单设计模式之工厂模式:类的泛化
1 public class OperationFactory
2 {
3 public static Operation createOperate(string operate)
4 {
5 Operation oper = null;
6 switch (operate)
7 {
8 case "+":
9 {
10 oper = new OperationAdd();
11 break;
12 }
13 case "-":
14 {
15 oper = new OperationSub();
16 break;
17 }
18 case "*":
19 {
20 oper = new OperationMul();
21 break;
22 }
23 case "/":
24 {
25 oper = new OperationDiv();
26 break;
27 }
28 }
29 return oper;
30 }
31 }
2 {
3 public static Operation createOperate(string operate)
4 {
5 Operation oper = null;
6 switch (operate)
7 {
8 case "+":
9 {
10 oper = new OperationAdd();
11 break;
12 }
13 case "-":
14 {
15 oper = new OperationSub();
16 break;
17 }
18 case "*":
19 {
20 oper = new OperationMul();
21 break;
22 }
23 case "/":
24 {
25 oper = new OperationDiv();
26 break;
27 }
28 }
29 return oper;
30 }
31 }
5、反射Using System.Reflection
Animal animal = (Animal)Assembly.Load("AnimalSystem").CreateInstance("AnimalSystem.Cat");