自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#調(diào)用構(gòu)造函數(shù)淺析

開發(fā) 后端
C#調(diào)用構(gòu)造函數(shù)是如何執(zhí)行的呢?C#調(diào)用構(gòu)造函數(shù)具體的會怎么操作呢?那么本文就向你介紹相關(guān)內(nèi)容。

C#調(diào)用構(gòu)造函數(shù)是如何的呢?讓我們首先來看看什么是構(gòu)造函數(shù)?

C#調(diào)用構(gòu)造函數(shù)之構(gòu)造函數(shù)定義:它是在創(chuàng)建給定類型的對象時執(zhí)行的類方法。構(gòu)造函數(shù)具有與類相同的名稱,它通常初始化新對象的數(shù)據(jù)成員。

在下面的示例中,定義了一個具有一個簡單的構(gòu)造函數(shù),名為 Taxi 的類。然后使用 new 運(yùn)算符來實(shí)例化該類。在為新對象分配內(nèi)存之后,new 運(yùn)算符立即調(diào)用 Taxi 構(gòu)造函數(shù)。

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. public class Taxi  
  2. {  
  3. public bool isInitialized;  
  4. public Taxi()  
  5. {  
  6. isInitialized = true;  
  7. }  
  8. }  
  9.  
  10. class TestTaxi  
  11. {  
  12. static void Main()  
  13. {  
  14. Taxi t = new Taxi();  
  15. System.Console.WriteLine(t.isInitialized);  
  16. }  

不帶參數(shù)的構(gòu)造函數(shù)稱為“默認(rèn)構(gòu)造函數(shù)”。無論何時,只要使用 new 運(yùn)算符實(shí)例化對象,并且不為 new 提供任何參數(shù),就會調(diào)用默認(rèn)構(gòu)造函數(shù)。有關(guān)更多信息,請參見 實(shí)例構(gòu)造函數(shù)。

除非類是 static 的,否則 C# 編譯器將為無構(gòu)造函數(shù)的類提供一個公共的默認(rèn)構(gòu)造函數(shù),以便該類可以實(shí)例化。有關(guān)更多信息,請參見 靜態(tài)類和靜態(tài)類成員。

通過將構(gòu)造函數(shù)設(shè)置為私有構(gòu)造函數(shù),可以阻止類被實(shí)例化,如下所示:

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. class NLog  
  2. {  
  3. // Private Constructor:  
  4. private NLog() { }  
  5.  
  6. public static double e = System.Math.E;  //2.71828...  

有關(guān)更多信息,請參見 私有構(gòu)造函數(shù)。

結(jié)構(gòu)類型的構(gòu)造函數(shù)與類的構(gòu)造函數(shù)類似,但是 structs 不能包含顯式默認(rèn)構(gòu)造函數(shù),因?yàn)榫幾g器將自動提供一個構(gòu)造函數(shù)。此構(gòu)造函數(shù)將結(jié)構(gòu)中的每個字段初始化為 默認(rèn)值表中顯示的默認(rèn)值。然而,只有當(dāng)結(jié)構(gòu)用 new 實(shí)例化時,才會調(diào)用此默認(rèn)構(gòu)造函數(shù)。例如,下面的代碼使用 Int32 的默認(rèn)構(gòu)造函數(shù),因此您可以確信整數(shù)已初始化:

  1. int i = new int();  
  2. Console.WriteLine(i); 

然而,下面的代碼卻導(dǎo)致了 編譯器錯誤 CS0165,因?yàn)樗鼪]有使用 new,而且試圖使用尚未初始化的對象:

  1. int i;  
  2. Console.WriteLine(i); 

基于 structs 的對象可以初始化或賦值后使用,如下所示:

  1. int a = 44;  // Initialize the value type...  
  2. int b;  
  3. b = 33;  // Or assign it before using it.  
  4. Console.WriteLine("{0}, {1}", a, b); 

因此對值類型調(diào)用默認(rèn)構(gòu)造函數(shù)不是必需的。

類和 structs 都可以定義具有參數(shù)的構(gòu)造函數(shù)。帶參數(shù)的構(gòu)造函數(shù)必須通過 new 語句或 base 語句來調(diào)用。類和 structs 還可以定義多個構(gòu)造函數(shù),并且二者均不需要定義默認(rèn)構(gòu)造函數(shù)。例如:

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. public class Employee  
  2. {  
  3. public int salary;  
  4.  
  5. public Employee(int annualSalary)  
  6. {  
  7. salary = annualSalary;  
  8. }  
  9.  
  10. public Employee(int weeklySalary, int numberOfWeeks)  
  11. {  
  12. salary = weeklySalary * numberOfWeeks;  
  13. }  

此類可以使用下列語句中的任一個來創(chuàng)建:

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. Employee e1 = new Employee(30000);  
  2. Employee e2 = new Employee(500, 52); 

構(gòu)造函數(shù)可以使用 base 關(guān)鍵字來調(diào)用基類的構(gòu)造函數(shù)。例如:

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. public class Manager : Employee  
  2. {  
  3. public Manager(int annualSalary)  
  4. base(annualSalary)  
  5. {  
  6. //Add further instructions here.  
  7. }  

在此示例中,基類的構(gòu)造函數(shù)在執(zhí)行構(gòu)造函數(shù)塊之前被調(diào)用。base 關(guān)鍵字可帶參數(shù)使用,也可不帶參數(shù)使用。構(gòu)造函數(shù)的任何參數(shù)都可用作 base 的參數(shù),或用作表達(dá)式的一部分。有關(guān)更多信息,請參見 base。

在派生類中,如果不使用 base 關(guān)鍵字來顯式調(diào)用基類構(gòu)造函數(shù),則將隱式調(diào)用默認(rèn)構(gòu)造函數(shù)(如果有的話)。這意味著下面的構(gòu)造函數(shù)聲明在效果上是相同的:

C#調(diào)用構(gòu)造函數(shù)實(shí)例

  1. public Manager(int initialdata)  
  2. {  
  3. //Add further instructions here.  
  4. }  
  5.  
  6. public Manager(int initialdata) : base()  
  7. {  
  8. //Add further instructions here.  

如果基類沒有提供默認(rèn)構(gòu)造函數(shù),派生類必須使用 base 顯式調(diào)用基構(gòu)造函數(shù)。

構(gòu)造函數(shù)可以使用 this 關(guān)鍵字調(diào)用同一對象中的另一構(gòu)造函數(shù)。和 base 一樣,this 可帶參數(shù)使用也可不帶參數(shù)使用,構(gòu)造函數(shù)中的任何參數(shù)都可用作 this 的參數(shù),或者用作表達(dá)式的一部分。例如,可以使用 this 重寫前一示例中的第二個構(gòu)造函數(shù):

  1. public Employee(int weeklySalary, int numberOfWeeks)  
  2. this(weeklySalary * numberOfWeeks)  
  3. {  

上面對 this 關(guān)鍵字的使用導(dǎo)致此構(gòu)造函數(shù)被調(diào)用:

  1. public Employee(int annualSalary)  
  2. {  
  3. salary = annualSalary;  

構(gòu)造函數(shù)可以標(biāo)記為 public、 private、 protected、 internal 或 protectedinternal。這些訪問修飾符定義類的用戶構(gòu)造該類的方式。有關(guān)更多信息,請參見 訪問修飾符。

使用 static 關(guān)鍵字可以將構(gòu)造函數(shù)聲明為靜態(tài)構(gòu)造函數(shù)。在訪問任何靜態(tài)字段之前,都將自動調(diào)用靜態(tài)構(gòu)造函數(shù),它們通常用于初始化靜態(tài)類成員。有關(guān)更多信息,請參見 靜態(tài)構(gòu)造函數(shù)。

C#調(diào)用構(gòu)造函數(shù)的相關(guān)內(nèi)容就向你介紹到這里,希望對你學(xué)習(xí)和了解C#調(diào)用構(gòu)造函數(shù)有所幫助。

【編輯推薦】

  1. 學(xué)習(xí)C#構(gòu)造函數(shù)的一點(diǎn)體會
  2. C#靜態(tài)構(gòu)造函數(shù)特點(diǎn)淺析
  3. C#靜態(tài)構(gòu)造函數(shù)學(xué)習(xí)心得淺析
  4. C#繼承構(gòu)造函數(shù)實(shí)現(xiàn)淺析
  5. C#繼承與構(gòu)造函數(shù)的調(diào)用實(shí)例演示
責(zé)任編輯:仲衡 來源: MSDN
相關(guān)推薦

2009-08-13 18:36:36

C#繼承構(gòu)造函數(shù)

2009-07-31 15:44:02

C#靜態(tài)構(gòu)造函數(shù)

2009-08-13 18:15:06

C#繼承構(gòu)造函數(shù)

2009-08-13 17:38:42

C#構(gòu)造函數(shù)

2009-09-18 09:02:45

CLR Via C#

2009-08-13 18:02:11

C#靜態(tài)構(gòu)造函數(shù)

2009-08-14 09:43:59

C#復(fù)制構(gòu)造函數(shù)

2009-08-14 09:58:09

C#復(fù)制構(gòu)造函數(shù)

2009-08-13 13:42:54

C#構(gòu)造函數(shù)

2009-08-13 17:30:30

C#構(gòu)造函數(shù)

2009-08-14 09:50:46

C#復(fù)制構(gòu)造函數(shù)

2009-08-13 18:10:31

C#靜態(tài)構(gòu)造函數(shù)

2009-08-04 09:30:33

C#調(diào)用ImageAn

2009-08-24 13:41:23

C# 泛型約束

2009-08-10 14:43:03

C#函數(shù)Convert

2009-07-31 16:00:30

C#函數(shù)重載

2009-07-31 14:03:21

C# Format函數(shù)

2011-06-11 21:36:44

C#C++

2009-07-31 14:15:38

C# 構(gòu)造函數(shù)

2009-08-24 18:09:13

C#構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號