基类 public class BaseName { //parts stored here protected string enName, cnName; protected DataTable dt = null; public string GetENName() { return enName; } public string GetCNName() { return cnName; } public DataTable GetDataByName() { return dt; } } 子类 public class Hotel:BaseName { public Hotel(string name) { int i = name.IndexOf (" "); if (i > 0) { enName = name.Substring(0, i).Trim(); cnName = name.Substring(i + 1).Trim(); } else { enName = ""; cnName = name; } dt = this.GetData(); } private DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("name", typeof(string)); DataRow row = null; for (int i = 0; i < 3; i++) { row = dt.NewRow(); row["name"] = "hotel" + i + enName + cnName; dt.Rows.Add(row); } return dt; } } public class Room:BaseName { public Room(string name) { int i = name.IndexOf(" "); if (i > 0) { enName = name.Substring(0, i).Trim(); cnName = name.Substring(i + 1).Trim(); } else { enName = ""; cnName = name; } dt = this.GetData(); } private DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("name", typeof(string)); DataRow row = null; for (int i = 0; i < 3; i++) { row = dt.NewRow(); row["name"] = "Room" + i + enName + cnName; dt.Rows.Add(row); } return dt; } } public class MyNameFactory { public MyNameFactory() { } public BaseName GetName(string name,int type) { switch (type) { case 1: return new Hotel(name); case 2: return new Room(name); default: return new Hotel(name); } } } 调用 MyNameFactory myNameFactory = new MyNameFactory(); BaseName baseName = myNameFactory.GetName(txName.Text, 1); string cnName = baseName.GetCNName(); string enName = baseName.GetENName(); DataTable dt = baseName.GetDataByName(); BaseName baseName2 = myNameFactory.GetName(txName.Text, 2); string cnName2 = baseName2.GetCNName(); string enName2 = baseName2.GetENName(); DataTable dt2 = baseName2.GetDataByName();