欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#简单工厂案例

程序员文章站 2022-07-01 16:17:36
using System; namespace Application { class JianDanGongChang { static void Main(string[] args) { Factory factory=new Factory(); DianNao diannao=factor... ......
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;
        }
    }
    
}