1.创建一个笔记本电脑父类
public abstract class NoteBook
{
public abstract void SayHello();
}
2.创建几个不同品牌的笔记本电脑子类
public class Lenovo:NoteBook
{
public override void SayHello()
{
Console.WriteLine(\”我是联想笔记本\”);
}
}
public class Dell:NoteBook
{
public override void SayHello()
{
Console.WriteLine(\”我是戴尔笔记本\”);
}
}
public class IBM:NoteBook
{
public override void SayHello()
{
Console.WriteLine(\”我是IBM笔记本\”);
}
}
3.接下来是关键的方法,根据传入不同的参数,返回父类的实例
public static NoteBook GetNoteBook(string brand)
{
NoteBook nb =null;
switch(brand)
{
case \”Lenovo\”:
nb = new Lenovo();
break;
case \”Dell\”:
nb = new Dell();
break;
case \”IBM\”:
nb = new IBM();
break;
}
return nb;
}
4.通过输入进行调用
static void Main(string[] args)
{
Console.WriteLine(\”请输入你需要选择的笔记本型号\”);
string sNBType = Console.ReadLine();
NoteBook nb = GetNoteBook(sNBType);
nb.SayHello();
Console.ReadLine();
}