微信交流Python、c#:15188607997
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04简单工厂设计模式
{
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 "IBM": nb = new IBM();
break;
case "Lenovo": nb = new Lenovo();
break;
case "Acer":nb = new Acer();
break;
case "Dell": nb = new Dell();
break;
}
return nb;
}
}
public abstract class NoteBook
{
public abstract void SayHello();
}
public class IBM : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是IBM");
}
}
public class Lenovo : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Lenovo");
}
}
public class Dell : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Dell");
}
}
public class Acer : NoteBook
{
public override void SayHello()
{
Console.WriteLine("我是Acer");
}
}
}