using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _10_单例模式 { class DBDA { public string host; //静态成员,用来存储该类的对象 public static DBDA d = null; //构造函数:让该类不能被实例化 private DBDA() { } //提供一个造对象的方法 ,控制只能造一个对象 public static DBDA DuiXiang() { if (d == null) { d = new DBDA(); } return d; } } class Program { static void Main(string[] args) { //调用DBDA方法、并赋值 DBDA d = DBDA.DuiXiang(); d.host = "localhost"; //再次调用DBDA方法、并赋值 DBDA d1 = DBDA.DuiXiang(); //输出第二次赋值的结果 Console.WriteLine(d1.host); Console.ReadKey(); } } }