詳解C#編程中構(gòu)造函數(shù)的使用
當(dāng)類或結(jié)構(gòu)創(chuàng)建時(shí),其構(gòu)造函數(shù)調(diào)用。構(gòu)造函數(shù)與選件類或結(jié)構(gòu)相同,并且,它們通常用于初始化新對(duì)象的數(shù)據(jù)成員。
在下面的示例中,使用一個(gè)簡(jiǎn)單的構(gòu)造函數(shù)定義了名為 Taxi 的類。然后使用 new 運(yùn)算符來(lái)實(shí)例化該類。在為新對(duì)象分配內(nèi)存之后,new 運(yùn)算符立即調(diào)用 Taxi 構(gòu)造函數(shù)。
public class Taxi
{
public bool isInitialized;
public Taxi()
{
isInitialized = true;
}
}
class TestTaxi
{
static void Main()
{
Taxi t = new Taxi();
Console.WriteLine(t.isInitialized);
}
}
不帶參數(shù)的構(gòu)造函數(shù)稱為“默認(rèn)構(gòu)造函數(shù)”。無(wú)論何時(shí),只要使用 new 運(yùn)算符實(shí)例化對(duì)象,并且不為 new 提供任何參數(shù),就會(huì)調(diào)用默認(rèn)構(gòu)造函數(shù)。
除非類是 static 的,否則 C# 編譯器將為無(wú)構(gòu)造函數(shù)的類提供一個(gè)公共的默認(rèn)構(gòu)造函數(shù),以便該類可以實(shí)例化。
通過(guò)將構(gòu)造函數(shù)設(shè)置為私有構(gòu)造函數(shù),可以阻止類被實(shí)例化,如下所示:
class NLog
{
// Private Constructor:
private NLog() { }
public static double e = Math.E; //2.71828...
}
結(jié)構(gòu)類型的構(gòu)造函數(shù)與類的構(gòu)造函數(shù)類似,但是 structs 不能包含顯式默認(rèn)構(gòu)造函數(shù),因?yàn)榫幾g器將自動(dòng)提供一個(gè)構(gòu)造函數(shù)。此構(gòu)造函數(shù)會(huì)將 struct 中的每個(gè)字段初始化為默認(rèn)值。然而,只有當(dāng) struct 用 new 實(shí)例化時(shí),才會(huì)調(diào)用此默認(rèn)構(gòu)造函數(shù)。例如,下面的代碼使用 Int32 的默認(rèn)構(gòu)造函數(shù),因此您可以確信整數(shù)已初始化:
int i = new int(); Console.WriteLine(i);
不過(guò),下面的代碼卻會(huì)導(dǎo)致編譯器錯(cuò)誤,因?yàn)樗鼪](méi)有使用 new,而且嘗試使用尚未初始化的對(duì)象:
int i; Console.WriteLine(i);
或者,基于 structs 的對(duì)象(包括所有內(nèi)置數(shù)值類型)可以初始化或賦值后使用,如下面的示例所示:
int a = 44; // Initialize the value type...
int b;
b = 33; // Or assign it before using it.
Console.WriteLine("{0}, {1}", a, b);
因此對(duì)值類型調(diào)用默認(rèn)構(gòu)造函數(shù)不是必需的。
類和 structs 都可以定義具有參數(shù)的構(gòu)造函數(shù)。帶參數(shù)的構(gòu)造函數(shù)必須通過(guò) new 語(yǔ)句或 base 語(yǔ)句來(lái)調(diào)用。類和 structs 還可以定義多個(gè)構(gòu)造函數(shù),并且二者均不需要定義默認(rèn)構(gòu)造函數(shù)。例如:
public class Employee
{
public int salary;
public Employee(int annualSalary)
{
salary = annualSalary;
}
public Employee(int weeklySalary, int numberOfWeeks)
{
salary = weeklySalary * numberOfWeeks;
}
}
可以使用下列語(yǔ)句中的任一個(gè)語(yǔ)句來(lái)創(chuàng)建此類:
Employee e1 = new Employee(30000); Employee e2 = new Employee(500, 52);
構(gòu)造函數(shù)可以使用 base 關(guān)鍵字來(lái)調(diào)用基類的構(gòu)造函數(shù)。例如:
public class Manager : Employee
{
public Manager(int annualSalary)
: base(annualSalary)
{
//Add further instructions here.
}
}
在此示例中,基類的構(gòu)造函數(shù)在執(zhí)行構(gòu)造函數(shù)塊之前被調(diào)用。 base 關(guān)鍵字可帶參數(shù)使用,也可不帶參數(shù)使用。構(gòu)造函數(shù)的任何參數(shù)都可用作 base 的參數(shù),或用作表達(dá)式的一部分。有關(guān)更多信息,請(qǐng)參見(jiàn)base(C# 參考)。
在派生類中,如果不使用 base 關(guān)鍵字來(lái)顯式調(diào)用基類構(gòu)造函數(shù),則將隱式調(diào)用默認(rèn)構(gòu)造函數(shù)(如果有的話)。這意味著下面的構(gòu)造函數(shù)聲明在效果上是相同的:
public Manager(int initialdata)
{
//Add further instructions here.
}
public Manager(int initialdata)
: base()
{
//Add further instructions here.
}
如果基類沒(méi)有提供默認(rèn)構(gòu)造函數(shù),派生類必須使用 base 顯式調(diào)用基構(gòu)造函數(shù)。
構(gòu)造函數(shù)可以使用 this 關(guān)鍵字調(diào)用同一對(duì)象中的另一構(gòu)造函數(shù)。和 base 一樣,this 可帶參數(shù)使用也可不帶參數(shù)使用,構(gòu)造函數(shù)中的任何參數(shù)都可用作 this 的參數(shù),或者用作表達(dá)式的一部分。例如,可以使用 this 重寫(xiě)前一示例中的第二個(gè)構(gòu)造函數(shù):
public Employee(int weeklySalary, int numberOfWeeks)
: this(weeklySalary * numberOfWeeks)
{
}
上一示例中對(duì) this 關(guān)鍵字的使用導(dǎo)致此構(gòu)造函數(shù)被調(diào)用:
public Employee(int annualSalary)
{
salary = annualSalary;
}
構(gòu)造函數(shù)可以標(biāo)記為 public、private、protected、internal 或 protectedinternal。這些訪問(wèn)修飾符定義類的用戶構(gòu)造該類的方式。有關(guān)更多信息,請(qǐng)參見(jiàn)訪問(wèn)修飾符。
實(shí)例構(gòu)造函數(shù)
使用 new 表達(dá)式創(chuàng)建某個(gè)類的對(duì)象時(shí),會(huì)使用實(shí)例構(gòu)造函數(shù)創(chuàng)建和初始化所有實(shí)例成員變量。要初始化靜態(tài)類或非靜態(tài)類中的靜態(tài)變量,必須定義靜態(tài)構(gòu)造函數(shù)。
下面的示例演示實(shí)例構(gòu)造函數(shù):
class CoOrds
{
public int x, y;
// constructor
public CoOrds()
{
x = 0;
y = 0;
}
}
注意
為了清楚起見(jiàn),此類包含公共字段。建議在編程時(shí)不要使用公共字段,因?yàn)檫@種做法會(huì)使程序中任何位置的任何方法都可以不受限制、不經(jīng)驗(yàn)證地訪問(wèn)對(duì)象的內(nèi)部組件。數(shù)據(jù)成員通常應(yīng)當(dāng)為私有的,并且只應(yīng)當(dāng)通過(guò)類方法和屬性來(lái)訪問(wèn)。
只要?jiǎng)?chuàng)建基于 CoOrds 類的對(duì)象,就會(huì)調(diào)用此實(shí)例構(gòu)造函數(shù)。諸如此類不帶參數(shù)的構(gòu)造函數(shù)稱為“默認(rèn)構(gòu)造函數(shù)”。然而,提供其他構(gòu)造函數(shù)通常十分有用。例如,可以向 CoOrds 類添加構(gòu)造函數(shù),以便可以為數(shù)據(jù)成員指定初始值:
// A constructor with two arguments:
public CoOrds(int x, int y)
{
this.x = x;
this.y = y;
}
這樣便可以用默認(rèn)或特定的初始值創(chuàng)建 CoOrd 對(duì)象,如下所示:
CoOrds p1 = new CoOrds(); CoOrds p2 = new CoOrds(5, 3);
如果某個(gè)類沒(méi)有構(gòu)造函數(shù),則會(huì)自動(dòng)生成一個(gè)默認(rèn)構(gòu)造函數(shù),并使用默認(rèn)值來(lái)初始化對(duì)象字段。例如,int 初始化為 0。有關(guān)默認(rèn)值的更多信息,請(qǐng)參見(jiàn) 默認(rèn)值表(C# 參考)。因此,由于 CoOrds 類的默認(rèn)構(gòu)造函數(shù)將所有數(shù)據(jù)成員都初始化為零,因此可以將它完全移除,而不會(huì)更改類的工作方式。本主題的稍后部分的示例 1 中提供了使用多個(gè)構(gòu)造函數(shù)的完整示例,示例 2 中提供了自動(dòng)生成的構(gòu)造函數(shù)的示例。
也可以用實(shí)例構(gòu)造函數(shù)來(lái)調(diào)用基類的實(shí)例構(gòu)造函數(shù)。類構(gòu)造函數(shù)可通過(guò)初始值設(shè)定項(xiàng)來(lái)調(diào)用基類的構(gòu)造函數(shù),如下所示:
class Circle : Shape
{
public Circle(double radius)
: base(radius, 0)
{
}
}
在此示例中,Circle 類將表示半徑和高度的值傳遞給 Shape(Circle 從它派生而來(lái))提供的構(gòu)造函數(shù)。使用 Shape 和 Circle 的完整示例請(qǐng)見(jiàn)本主題中的示例 3。
示例 1
下面的示例說(shuō)明包含兩個(gè)類構(gòu)造函數(shù)的類:一個(gè)類構(gòu)造函數(shù)沒(méi)有參數(shù),另一個(gè)類構(gòu)造函數(shù)帶有兩個(gè)參數(shù)。
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 沒(méi)有任何構(gòu)造函數(shù);在這種情況下,將自動(dòng)提供默認(rèn)構(gòu)造函數(shù),同時(shí)將字段初始化為它們的默認(rèn)值。
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 的默認(rèn)值為 0,name 的默認(rèn)值為 null。有關(guān)默認(rèn)值的更多信息,請(qǐng)參見(jiàn) 默認(rèn)值表(C# 參考)。
示例 3
下面的示例說(shuō)明使用基類初始值設(shè)定項(xiàng)。 Circle 類是從通用類 Shape 派生的,Cylinder 類是從 Circle 類派生的。每個(gè)派生類的構(gòu)造函數(shù)都使用其基類的初始值設(shè)定項(xiàng)。
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#構(gòu)造函數(shù)詳解
- C# 構(gòu)造函數(shù)如何調(diào)用虛方法
- 淺談C# 構(gòu)造方法(函數(shù))
- C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列示例詳解
- C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實(shí)例詳解
- C#靜態(tài)構(gòu)造函數(shù)用法實(shí)例分析
- C#中靜態(tài)構(gòu)造函數(shù)的幾點(diǎn)說(shuō)明介紹
- C#私有構(gòu)造函數(shù)使用示例
- c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實(shí)例
- C# 靜態(tài)構(gòu)造函數(shù)使用總結(jié)
- C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
相關(guān)文章
.NET單點(diǎn)登陸的實(shí)現(xiàn)方法及思路
這篇文章介紹了.NET單點(diǎn)登陸的實(shí)現(xiàn)方法及思路,有需要的朋友可以參考一下,希望對(duì)你有所幫助2013-07-07
C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
這篇文章主要介紹了C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C# winForm實(shí)現(xiàn)的氣泡提示窗口功能示例
這篇文章主要介紹了C# winForm實(shí)現(xiàn)的氣泡提示窗口功能,涉及C# winForm窗口屬性與設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
C#實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
詳解C# parallel中并行計(jì)算的四種寫(xiě)法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
c#關(guān)于非托管內(nèi)存的釋放問(wèn)題及解讀
這篇文章主要介紹了c#關(guān)于非托管內(nèi)存的釋放問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

