C#简单工厂设计模式

来源:互联网 发布:程序员专用字体 编辑:程序博客网 时间:2024/06/02 19:21
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 简单工厂模式{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("请输入笔记本品牌:");            string Brand = Console.ReadLine();            NoteBook nb = GetNoteBook(Brand);            nb.SayHello();            Console.ReadKey();        }        //简单工厂的核心,根据用户的输入创建对象赋值给父类        public static NoteBook GetNoteBook(string brand)        {            NoteBook nb = null;            switch (brand)            {                case "Dell": nb = new Dell();                    break;                case "IBM": nb = new IBM();                    break;                case "Acer": nb = new Acer();                    break;                default:                    break;            }            return nb;        }    }    public abstract class NoteBook    {        public abstract void SayHello();    }    public class Acer : NoteBook    {        public override void SayHello()        {            Console.WriteLine("我是宏碁");        }    }    public class IBM : NoteBook    {        public override void SayHello()        {            Console.WriteLine("我是IBM笔记本");        }    }    public class Dell : NoteBook    {        public override void SayHello()        {            Console.WriteLine("我是戴尔笔记本");        }    }}
0 0
原创粉丝点击