1.構造函數是一個特殊的方法,沒有返回值類型,不能返回值,方法名稱和類名一致。
2.構造函數的作用就是構建對象的屬性值。
3.任何類的成員都不可能與類名稱一樣,除了構造函數與析構函數。
4.任何類都會默認提供一個無參的構造函數。但是如果手動添加了帶參的構造函數,那么無參的構造函數,會自動消失,所以在創建類之后,立馬為其添加一個無參的構造函數。
不添加的話,在下面的例子中會有問題。
5.構造函數本身也是一個方法,所以可以重載,但是不能像普通方法一樣的調用,它只有兩個場合可以使用
*創建對象new的時候
*構造函數之間的相互調用,同一個類的構造函數調用(使用this關鍵字);子類調用父類的構造函數(使用base關鍵字)。
*同一個類的構造函數的相互調用,將接收到的實參值再傳遞給另外一個構造函數。
*在new一個子類的構造函數的時候,先會去創建父類的構造函數,再來創建自己的構造函數
*構造函數不可以繼承,因為構造函數就是構造每個對象自己的方法,但是構造函數是可以來調用的。
*父類在幫子類構造的時候,不能使用子類非公共成員,否則就循環依賴了。
*子類構造函數默認調用父類的無參構造函數,如果父類沒有無參構造函數,就必須指明調用父類哪個構造函數。
*如果子類重寫了父類的成員,那么this.成員。調用的是子類重寫以后的。base.成員調用的依然是父類的成員。
當存在有參的構造函數之后,無參的構造函數自動消失了,必須要手動添加無參的構造函數。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 屬性的封裝 8 { 9 class Teacher 10 { 11 /// <summary> 12 /// 默認的無參構造函數 13 /// </summary> 14 public Teacher() { } 15 16 17 /// <summary> 18 /// 19 /// </summary> 20 /// <param name="phone"></param> 21 public Teacher(int age) 22 { 23 this.Age = age; 24 } 25 26 /// <summary> 27 /// 構造函數 28 /// </summary> 29 /// <param name="name"></param> 30 public Teacher(string name, string sex, int age):this(age) 31 { 32 this.Name = name; 33 Sex = sex; 34 // Age = age; 35 } 36 37 /// <summary> 38 /// 39 /// </summary> 40 /// <param name="name"></param> 41 /// <param name="sex"></param> 42 /// <param name="age"></param> 43 /// <param name="phone"></param> 44 /// <param name="subject"></param> 45 public Teacher(string name, string sex, int age, string phone, string subject):this(name,sex,age) 46 { 47 //this.Name = name; 48 //this.Sex = sex; 49 //this.Age = age; 50 this.Phone = phone; 51 this.Subject = subject; 52 } 53 54 55 56 57 58 #region 工號 59 /// <summary> 60 /// 工號 61 /// </summary> 62 int id; 63 64 public int Id 65 { 66 get { return id; } 67 set { id = value; } 68 } 69 #endregion 70 71 #region 姓名 72 /// <summary> 73 /// 姓名 74 /// </summary> 75 string name; 76 77 public string Name 78 { 79 get { return name; } 80 set { name = value; } 81 } 82 #endregion 83 84 #region 性別 85 /// <summary> 86 /// 性別 87 /// </summary> 88 string sex; 89 90 public string Sex 91 { 92 get { return sex; } 93 set 94 { 95 if (value == "男" || value == "女") 96 { 97 sex = value; 98 } 99 else 100 { 101 sex = "人妖"; 102 } 103 104 } 105 } 106 #endregion 107 108 #region 年齡 109 /// <summary> 110 /// 年齡 111 /// </summary> 112 int age; 113 114 public int Age 115 { 116 get { return age; } 117 set 118 { 119 if (value > 0 && value <= 100) 120 { 121 age = value; 122 } 123 else 124 { 125 age = 18; 126 } 127 128 } 129 } 130 #endregion 131 132 #region 課程 133 /// <summary> 134 /// 課程 135 /// </summary> 136 string subject; 137 138 public string Subject 139 { 140 get { return subject; } 141 set { subject = value; } 142 } 143 #endregion 144 145 #region 電話號碼 146 /// <summary> 147 /// 電話號碼 148 /// </summary> 149 string phone; 150 151 public string Phone 152 { 153 get { return phone; } 154 set { phone = value; } 155 } 156 #endregion 157 158 #region Show 159 /// <summary> 160 /// Show 161 /// </summary> 162 public void Show() 163 { 164 Console.WriteLine("大家好,我是你們的老師,我叫{0} ,我的性別是{1} ,年齡是{2},我教的課程是{3}", this.Name, this.Sex, this.Age, this.Subject); 165 } 166 167 #endregion 168 } 169 }

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 屬性的封裝 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 // 15 Teacher t = new Teacher("ccc","sss",22); 16 t.Show(); 17 Teacher t1 = new Teacher("fangfang", "girl", 22, "12234242342", "語文"); 18 t1.Show(); 19 Console.ReadKey(); 20 } 21 } 22 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 屬性封裝 { class Student:People { public Student() { } public Student(string name, int age) { this.Name = name; this.Age = age; } public Student(string name, string sex, int age):this(name,age) { //this.Name = name; this.Sex = sex; //this.Age = age; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 屬性封裝 { class Program { static void Main(string[] args) { //Student stu = new Student() //{ // ID=1, // Name="mike", // Sex="nan", // Age=233 //}; //stu.Show(); Student stu1 = new Student("ccc", "nan", 12); Console.ReadKey(); } } }
這里測試的時候,是調用帶有3個參數的構造函數,
public Student(string name, string sex, int age):this(name,age)
{
//this.Name = name;
this.Sex = sex;
//this.Age = age;
}
執行到這個構造函數的時候,不會先進來這個構造函數的方法體,而是先去this里面,即調用帶有兩個參數的構造函數。
public Student(string name, int age) { this.Name = name; this.Age = age; }
這里的aa是瞞天過海,我們調用的時候, Teacher ta = new Teacher("ccc", "nan", 12);,傳遞的是,ccc,結果到了那,給偷工減料,傳遞aa了。。
文章列表