namespace 简单工厂模式1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你要选择的笔记本");
string str = Console.ReadLine();
Notebook nb = GetNoteBook(str);
nb.Showpai();
Console.ReadKey();
}
/// <summary>
/// 输入品牌,并选择实例化
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static Notebook GetNoteBook(string str)
{
Notebook nb = null;//如果不赋值,系统就会报错并非所有代码都有返回值,赋值NULL不占用内存
switch (str)
{
case "Hp": nb = new Hp();
break;
case "Sony": nb = new Sony();
break;
case "Lenvor": nb = new Lenvor();
break;
case "Acer": nb = new Acer();
break;
}
return nb;
}
}
public class Acer:Notebook
{
public override void Showpai()
{
Console.WriteLine("我是宏基笔记本");
}
}
public class Hp:Notebook
{
public override void Showpai()
{
Console.WriteLine("我是惠普笔记本");
}
}
public class Lenvor:Notebook
{
public override void Showpai()
{
Console.WriteLine("我是联想笔记本");
}
}
public abstract class Notebook
{
public abstract void Showpai();
}
public class Sony:Notebook
{
public override void Showpai()
{
Console.WriteLine("我是索尼笔记本");
}
}
|