1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace SimpleFactoryDemo
6 {
7 //创建抽象员工类
8 public abstract class Employees
9 {
10 public abstract void ShowInfo();
11 }
12
13
14 public class FullTimeEmployees:Employees
15 {
16
17 public override void ShowInfo()
18 {
19 Console.WriteLine("你好,我是全职员工");
20 }
21 }
22
23 public class PartTimeEmployees:Employees
24 {
25
26 public override void ShowInfo()
27 {
28 Console.WriteLine("你好,我是兼职员工");
29 }
30 }
31
32 public class Factory
33 {
34 public Employees CreateEmployees(int type)
35 {
36 switch (type)
37 {
38 case 1:
39 return new FullTimeEmployees();
40 case 2:
41 return new PartTimeEmployees();
42 default:
43 throw new ArgumentException("未知员工类型");
44 }
45 }
46 }
47
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 Console.WriteLine("请输入员工类型,1为全职,2为兼职");
53 int input = int.Parse(Console.ReadLine());
54 try
55 {
56 Factory fac = new Factory();
57 fac.CreateEmployees(input).ShowInfo();
58 }
59 catch (Exception ex)
60 {
61 Console.WriteLine(ex.Message);
62 }
63
64 }
65 }
66 }
67
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace SimpleFactoryDemo
6 {
7 //创建抽象员工类
8 public abstract class Employees
9 {
10 public abstract void ShowInfo();
11 }
12
13
14 public class FullTimeEmployees:Employees
15 {
16
17 public override void ShowInfo()
18 {
19 Console.WriteLine("你好,我是全职员工");
20 }
21 }
22
23 public class PartTimeEmployees:Employees
24 {
25
26 public override void ShowInfo()
27 {
28 Console.WriteLine("你好,我是兼职员工");
29 }
30 }
31
32 public class Factory
33 {
34 public Employees CreateEmployees(int type)
35 {
36 switch (type)
37 {
38 case 1:
39 return new FullTimeEmployees();
40 case 2:
41 return new PartTimeEmployees();
42 default:
43 throw new ArgumentException("未知员工类型");
44 }
45 }
46 }
47
48 class Program
49 {
50 static void Main(string[] args)
51 {
52 Console.WriteLine("请输入员工类型,1为全职,2为兼职");
53 int input = int.Parse(Console.ReadLine());
54 try
55 {
56 Factory fac = new Factory();
57 fac.CreateEmployees(input).ShowInfo();
58 }
59 catch (Exception ex)
60 {
61 Console.WriteLine(ex.Message);
62 }
63
64 }
65 }
66 }
67