1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 /*------------------------------------------------------------------------------------------------------------ 7 * 私有构造函数: 8 * 1. 只能在类内部调用 9 * 2. 通过类的方法返回类的实例 10 ------------------------------------------------------------------------------------------------------------*/ 11 namespace 类_私有构造函数 12 { 13 class Person 14 { 15 private string _name; 16 public string Name 17 { 18 get { return this._name; } 19 } 20 21 // 私有构造函数 22 private Person() 23 { 24 Console.WriteLine("私有构造函数被调用"); 25 this._name = "Hello World!"; 26 } 27 28 // 析构函数最后被自动调用 29 ~Person() 30 { 31 Console.WriteLine("Person析构函数被调用了!"); 32 } 33 34 // 必须使用静态方法 , 用于返回类的实例 35 public static Person GetInstance() 36 { 37 return new Person(); 38 } 39 40 } 41 42 class Program 43 { 44 static void Main(string[] args) 45 { 46 Person p = Person.GetInstance(); 47 48 Console.WriteLine("类实例的Name属性为: {0}", p.Name); 49 Console.ReadLine(); 50 } 51 } 52 }