C#简单工厂案例
using System;namespace Application
{
class JianDanGongChang
{
static void Main(string[] args)
{
Factory factory=new Factory();
DianNao diannao=factory.Create("Lx");
diannao.Print();
}
}
public abstract class DianNao
{
public abstract void Print();
}
public class LianXiang:DianNao
{
public override void Print()
{
Console.WriteLine("联想电脑");
}
}
public class DaiEr:DianNao
{
public override void Print()
{
Console.WriteLine("戴尔电脑");
}
}
//创建一个工厂类
public class Factory
{
private DianNao dianNao;
//创建一个工厂方法
public DianNao Create(string diannao){
switch(diannao)
{
case "Lx":dianNao=new LianXiang();
break;
case "De":dianNao=new DaiEr();
break;
default:
break;
}
return dianNao;
}
}
}
<p> </p><br>来源:<a href="https://www.cnblogs.com/dsd-dx/p/11338814.html" target="_blank">https://www.cnblogs.com/dsd-dx/p/11338814.html</a><br>免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]