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; } } }