Posted on 2017-09-15 22:14
viery 阅读(180)
评论(1) 编辑 收藏 所属分类:
.net
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SimpleFactory
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Console.WriteLine("请输入要制造的品牌");
14 string name = Console.ReadLine();
15 getNoteBook(name).makeNoteBook();
16 Console.ReadKey();
17 }
18
19
20 /// <summary>
21 /// 创建工厂方法
22 /// </summary>
23 /// <param name="name">要制造的品牌</param>
24 /// <returns>笔记本</returns>
25
26 public static NoteBook getNoteBook(string name)
27 {
28 NoteBook nb = null;
29
30 switch(name){
31 case"Lenovo":nb=new Lenovo();
32 break;
33 case "Dell": nb = new Dell();
34 break;
35 case "IBM": nb = new IBM();
36 break;
37 }
38 return nb;
39 }
40 }
41 //创建笔记本的父类
42
43 public abstract class NoteBook
44 {
45 public abstract void makeNoteBook();
46 }
47
48 //创建要生产的品牌
49 public class Lenovo : NoteBook
50 {
51 public override void makeNoteBook()
52 {
53 Console.Write("正在生产Lenovo笔记本");
54 }
55 }
56
57 public class Dell : NoteBook
58 {
59 public override void makeNoteBook()
60 {
61 Console.Write("正在生产Dell笔记本");
62 }
63 }
64
65 public class IBM : NoteBook
66 {
67 public override void makeNoteBook()
68 {
69 Console.Write("正在生产IBM笔记本");
70 }
71 }
72
73
74 }