C# 设计模式,简单工厂

C# 实现计算机功能 (封装,继承,多态)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单工厂 {
 8     class Program {
 9         
10         static void Main(string[] args) {
11             Operation op = Facotry.createFactory("+");
12             op.Number1 = 10;
13             op.Number2 = 20;
14             int result = op.getResult();
15             Console.WriteLine(op.ToString());
16             Console.ReadKey();
17         }
18         class Operation { //父类,封装
19             protected int number1;
20             protected int number2;
21 
22             public int Number1 {
23                 get { return number1; }
24                 set { number1 = value; }
25             }
26             public int Number2 {
27                 get { return number2; }
28                 set { number2 = value; }
29             }
30             public virtual int getResult() {
31                 int result = 0;
32                 return result;
33             }
34         }
35         class OperationAdd : Operation {
36             public override int getResult() {
37                 return number1 + number2;
38             }
39         }
40 
41         class OperationSub : Operation {
42             public override int getResult() {
43                 return number1-number2;
44             }
45         }
46         
47         class Facotry {
48             public static Operation createFactory(string ope) {
49                 Operation op = null;
50                 switch (ope) {
51                     case "+":
52                         op = new OperationAdd();
53                         break;
54                     case "-":
55                         op = new OperationSub();
56                         break;
57                 }
58                 return op;
59             }
60         }
61     }
62 }
View Code

 

转载于:https://www.cnblogs.com/zgrh/p/11114882.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值