詳解C#編程中構造函數的使用
當類或結構創(chuàng)建時,其構造函數調用。構造函數與選件類或結構相同,并且,它們通常用于初始化新對象的數據成員。
在下面的示例中,使用一個簡單的構造函數定義了名為 Taxi 的類。然后使用 new 運算符來實例化該類。在為新對象分配內存之后,new 運算符立即調用 Taxi 構造函數。
public class Taxi { public bool isInitialized; public Taxi() { isInitialized = true; } } class TestTaxi { static void Main() { Taxi t = new Taxi(); Console.WriteLine(t.isInitialized); } }
不帶參數的構造函數稱為“默認構造函數”。無論何時,只要使用 new 運算符實例化對象,并且不為 new 提供任何參數,就會調用默認構造函數。
除非類是 static 的,否則 C# 編譯器將為無構造函數的類提供一個公共的默認構造函數,以便該類可以實例化。
通過將構造函數設置為私有構造函數,可以阻止類被實例化,如下所示:
class NLog { // Private Constructor: private NLog() { } public static double e = Math.E; //2.71828... }
結構類型的構造函數與類的構造函數類似,但是 structs 不能包含顯式默認構造函數,因為編譯器將自動提供一個構造函數。此構造函數會將 struct 中的每個字段初始化為默認值。然而,只有當 struct 用 new 實例化時,才會調用此默認構造函數。例如,下面的代碼使用 Int32 的默認構造函數,因此您可以確信整數已初始化:
int i = new int(); Console.WriteLine(i);
不過,下面的代碼卻會導致編譯器錯誤,因為它沒有使用 new,而且嘗試使用尚未初始化的對象:
int i; Console.WriteLine(i);
或者,基于 structs 的對象(包括所有內置數值類型)可以初始化或賦值后使用,如下面的示例所示:
int a = 44; // Initialize the value type... int b; b = 33; // Or assign it before using it. Console.WriteLine("{0}, {1}", a, b);
因此對值類型調用默認構造函數不是必需的。
類和 structs 都可以定義具有參數的構造函數。帶參數的構造函數必須通過 new 語句或 base 語句來調用。類和 structs 還可以定義多個構造函數,并且二者均不需要定義默認構造函數。例如:
public class Employee { public int salary; public Employee(int annualSalary) { salary = annualSalary; } public Employee(int weeklySalary, int numberOfWeeks) { salary = weeklySalary * numberOfWeeks; } }
可以使用下列語句中的任一個語句來創(chuàng)建此類:
Employee e1 = new Employee(30000); Employee e2 = new Employee(500, 52);
構造函數可以使用 base 關鍵字來調用基類的構造函數。例如:
public class Manager : Employee { public Manager(int annualSalary) : base(annualSalary) { //Add further instructions here. } }
在此示例中,基類的構造函數在執(zhí)行構造函數塊之前被調用。 base 關鍵字可帶參數使用,也可不帶參數使用。構造函數的任何參數都可用作 base 的參數,或用作表達式的一部分。有關更多信息,請參見base(C# 參考)。
在派生類中,如果不使用 base 關鍵字來顯式調用基類構造函數,則將隱式調用默認構造函數(如果有的話)。這意味著下面的構造函數聲明在效果上是相同的:
public Manager(int initialdata) { //Add further instructions here. } public Manager(int initialdata) : base() { //Add further instructions here. }
如果基類沒有提供默認構造函數,派生類必須使用 base 顯式調用基構造函數。
構造函數可以使用 this 關鍵字調用同一對象中的另一構造函數。和 base 一樣,this 可帶參數使用也可不帶參數使用,構造函數中的任何參數都可用作 this 的參數,或者用作表達式的一部分。例如,可以使用 this 重寫前一示例中的第二個構造函數:
public Employee(int weeklySalary, int numberOfWeeks) : this(weeklySalary * numberOfWeeks) { }
上一示例中對 this 關鍵字的使用導致此構造函數被調用:
public Employee(int annualSalary) { salary = annualSalary; }
構造函數可以標記為 public、private、protected、internal 或 protectedinternal。這些訪問修飾符定義類的用戶構造該類的方式。有關更多信息,請參見訪問修飾符。
實例構造函數
使用 new 表達式創(chuàng)建某個類的對象時,會使用實例構造函數創(chuàng)建和初始化所有實例成員變量。要初始化靜態(tài)類或非靜態(tài)類中的靜態(tài)變量,必須定義靜態(tài)構造函數。
下面的示例演示實例構造函數:
class CoOrds { public int x, y; // constructor public CoOrds() { x = 0; y = 0; } }
注意
為了清楚起見,此類包含公共字段。建議在編程時不要使用公共字段,因為這種做法會使程序中任何位置的任何方法都可以不受限制、不經驗證地訪問對象的內部組件。數據成員通常應當為私有的,并且只應當通過類方法和屬性來訪問。
只要創(chuàng)建基于 CoOrds 類的對象,就會調用此實例構造函數。諸如此類不帶參數的構造函數稱為“默認構造函數”。然而,提供其他構造函數通常十分有用。例如,可以向 CoOrds 類添加構造函數,以便可以為數據成員指定初始值:
// A constructor with two arguments: public CoOrds(int x, int y) { this.x = x; this.y = y; }
這樣便可以用默認或特定的初始值創(chuàng)建 CoOrd 對象,如下所示:
CoOrds p1 = new CoOrds(); CoOrds p2 = new CoOrds(5, 3);
如果某個類沒有構造函數,則會自動生成一個默認構造函數,并使用默認值來初始化對象字段。例如,int 初始化為 0。有關默認值的更多信息,請參見 默認值表(C# 參考)。因此,由于 CoOrds 類的默認構造函數將所有數據成員都初始化為零,因此可以將它完全移除,而不會更改類的工作方式。本主題的稍后部分的示例 1 中提供了使用多個構造函數的完整示例,示例 2 中提供了自動生成的構造函數的示例。
也可以用實例構造函數來調用基類的實例構造函數。類構造函數可通過初始值設定項來調用基類的構造函數,如下所示:
class Circle : Shape { public Circle(double radius) : base(radius, 0) { } }
在此示例中,Circle 類將表示半徑和高度的值傳遞給 Shape(Circle 從它派生而來)提供的構造函數。使用 Shape 和 Circle 的完整示例請見本主題中的示例 3。
示例 1
下面的示例說明包含兩個類構造函數的類:一個類構造函數沒有參數,另一個類構造函數帶有兩個參數。
class CoOrds { public int x, y; // Default constructor: public CoOrds() { x = 0; y = 0; } // A constructor with two arguments: public CoOrds(int x, int y) { this.x = x; this.y = y; } // Override the ToString method: public override string ToString() { return (String.Format("({0},{1})", x, y)); } } class MainClass { static void Main() { CoOrds p1 = new CoOrds(); CoOrds p2 = new CoOrds(5, 3); // Display the results using the overriden ToString method: Console.WriteLine("CoOrds #1 at {0}", p1); Console.WriteLine("CoOrds #2 at {0}", p2); Console.ReadKey(); } }
輸出:
CoOrds #1 at (0,0) CoOrds #2 at (5,3)
示例 2
在此示例中,類 Person 沒有任何構造函數;在這種情況下,將自動提供默認構造函數,同時將字段初始化為它們的默認值。
public class Person { public int age; public string name; } class TestPerson { static void Main() { Person person = new Person(); Console.WriteLine("Name: {0}, Age: {1}", person.name, person.age); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
輸出:
Name: , Age: 0
注意,age 的默認值為 0,name 的默認值為 null。有關默認值的更多信息,請參見 默認值表(C# 參考)。
示例 3
下面的示例說明使用基類初始值設定項。 Circle 類是從通用類 Shape 派生的,Cylinder 類是從 Circle 類派生的。每個派生類的構造函數都使用其基類的初始值設定項。
abstract class Shape { public const double pi = Math.PI; protected double x, y; public Shape(double x, double y) { this.x = x; this.y = y; } public abstract double Area(); } class Circle : Shape { public Circle(double radius) : base(radius, 0) { } public override double Area() { return pi * x * x; } } class Cylinder : Circle { public Cylinder(double radius, double height) : base(radius) { y = height; } public override double Area() { return (2 * base.Area()) + (2 * pi * x * y); } } class TestShapes { static void Main() { double radius = 2.5; double height = 3.0; Circle ring = new Circle(radius); Cylinder tube = new Cylinder(radius, height); Console.WriteLine("Area of the circle = {0:F2}", ring.Area()); Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area()); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
輸出:
Area of the circle = 19.63 Area of the cylinder = 86.39
相關文章
C#中實現PriorityQueue優(yōu)先級隊列的代碼
這篇文章主要介紹了C#中PriorityQueue優(yōu)先級隊列的實現,構造初始化這部分主要介紹關鍵的字段和方法,比較器的初始化以及堆的初始化,需要的朋友可以參考下2021-12-12