建構函式在物件導向程式設計中扮演著非常重要的角色。讓我們透過以下幾點來了解建構子在 C# 中的作用:
文法:
public class Student() { //constructor public Student() { //code } }
這裡,public Student() 是沒有任何回傳類型,甚至沒有 void 的方法,它的名稱與類別名稱相同,即「Student」。因此,該方法是該類別的建構子。
當我們使用以下方法建立此類別的物件時:
Student obj = new Student();
然後將執行建構函式內的程式碼。
1. 建構子初始化新物件的資料成員。在記憶體分配給新物件後,它會立即由“new”運算符呼叫。
2. 明確建構子(使用者定義的建構子)可以是無參數的,也可以是參數化的。如果它是參數化的,那麼傳遞給建構函數的值可以分配給類別的資料成員。
3. 即使我們建立該類別的多個實例,隱式建構子也會以相同的值初始化該類別的變數。
範例:
代碼:
using System; public class ConstructorDemo { public int num = 10; public static void Main() { ConstructorDemo obj1 = new ConstructorDemo(); ConstructorDemo obj2 = new ConstructorDemo(); ConstructorDemo obj3 = new ConstructorDemo(); Console.WriteLine("obj1.num = "+obj1.num+"\nobj2.num = "+obj2.num +"\nobj3.num = "+obj3.num); } }
輸出:
上述程序的圖示:
4. 帶參數的明確建構子允許我們在每次建立該類別的實例時使用不同的值初始化該類別的變數。
範例:
代碼:
using System; public class ConstructorDemo { public int num; //explicit constructor public ConstructorDemo(int num) { this.num = num; } public static void Main() { ConstructorDemo obj1 = new ConstructorDemo(10); ConstructorDemo obj2 = new ConstructorDemo(20); ConstructorDemo obj3 = new ConstructorDemo(30); Console.WriteLine("obj1.num = "+obj1.num+"\nobj2.num = "+obj2.num +"\nobj3.num = "+obj3.num); } }
輸出:
上述程序的圖示:
C#提供了五種類型的建構子。它們如下:
範例:
代碼:
using System; public class DefaultConstructor { public int num; public string str; } public class Demo { public static void Main() { DefaultConstructor obj = new DefaultConstructor(); Console.WriteLine("obj.num = "+obj.num+"\nobj.str = "+obj.str); } }
輸出:
具有至少一個參數的建構子稱為參數化建構子。建立類別的實例時可以將參數傳遞給建構函式。它允許我們用不同的值初始化類別的每個實例。
範例:
代碼:
using System; public class ParameterizedConstructor { public int num; public string str; //parameterized constructor public ParameterizedConstructor(int num, string str) { this.num = num; this.str = str; } } public class Demo { public static void Main() { //passing values to constructor while creating instance ParameterizedConstructor obj = new ParameterizedConstructor(50, "constructor"); Console.WriteLine("obj.num = "+obj.num+"\nobj.str = "+obj.str); } }
輸出:
它是一個參數化建構函數,以同一個類別的物件作為參數。它將現有物件的值(作為參數傳遞)複製到建構函式實例化的新建立的物件。我們可以說它將一個物件的資料複製到另一個物件。
範例:
代碼:
using System; public class CopyConstructor { public int num; public CopyConstructor(int num) { this.num = num; } //copy constructor public CopyConstructor(CopyConstructor obj) { num = obj.num; } } public class Demo { public static void Main() { CopyConstructor obj1 = new CopyConstructor(50); //passing same class's object as parameter CopyConstructor obj2 = new CopyConstructor(obj1); Console.WriteLine("Original object:"); Console.WriteLine("obj1.num = "+obj1.num); Console.WriteLine("\nCopied object:"); Console.WriteLine("obj2.num = "+obj2.num); } }
輸出:
範例:
代碼:
using System; public class StaticConstructor { //static constructor static StaticConstructor() { Console.WriteLine("Static constructor executed"); } public static void Display() { Console.WriteLine("\nDisplay method executed"); } } public class Demo { public static void Main() { StaticConstructor.Display(); } }
輸出:
A constructor created with a private specifier is called a private constructor. We cannot create an instance of the class if it contains only a private constructor, and it does not allow other classes to derive from this class. Used in class that contains only static members.
Example:
Code:
using System; public class PrivateConstructor { public static int num = 100; //private constructor private PrivateConstructor() { } } public class Demo { public static void Main() { //PrivateConstructor obj = new PrivateConstructor(); //Error Console.WriteLine("num = "+PrivateConstructor.num); } }
Output:
If we define any type of constructor in a class, then there will not be any implicit constructor in the class provided by the compiler. Like methods, parameterized constructors can also be overloaded with different numbers of parameters. Constructors defined implicitly by the compiler are always public.
This is a guide to Constructor in C#. Here we discuss the types of Constructor in C# and its Work along with Code Implementation and Output. You can also go through our other suggested articles to learn more –
以上是C# 中的建構函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!