static NoteBook GetNoteBook(string brand) { NoteBook nb = null; switch (brand) { case "Acer": nb = new Acer(); break; case "IBM": nb = new IBM(); break; case "Lenovo": nb = new Lenovo(); break; case "Dell": nb = new Dell(); break; } return(nb); }
static void Main(string[] args) { Console.WriteLine("请输入你想要的笔记本"); string brand = Console.ReadLine(); NoteBook nb = GetNoteBook(brand); if (nb != null) { nb.SayHello(); } else { Console.WriteLine("没有你想要的笔记本"); } Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("请输入你想要的笔记本品牌"); string brand = Console.ReadLine(); //整个简单工厂的核心 工厂 //根据用户的输入 返回相应的笔记本 父类 NoteBook nb = GetNoteBook(brand); if (nb != null) { nb.SayHello(); } else { Console.WriteLine("没有你想要的电脑品牌!!!!"); } Console.ReadKey(); }