首頁  >  文章  >  後端開發  >  C# 中的建構函數

C# 中的建構函數

王林
王林原創
2024-09-03 15:12:28189瀏覽

建構函式在物件導向程式設計中扮演著非常重要的角色。讓我們透過以下幾點來了解建構子在 C# 中的作用:

  • 建構子是類別中存在的一種特殊方法,負責初始化類別變數。
  • 其名稱與類別名稱相同。
  • 當我們建立類別的實例時它會自動執行。
  • 建構函式不傳回任何值。
  • 如果我們不定義建構函數,則類別總是提供一個隱式建構函數,稱為預設建構函數。

文法:

public class Student()
{
//constructor
public Student()
{
//code
}
}

這裡,public Student() 是沒有任何回傳類型,甚至沒有 void 的方法,它的名稱與類別名稱相同,即「Student」。因此,該方法是該類別的建構子。

當我們使用以下方法建立此類別的物件時:

Student obj = new Student();

然後將執行建構函式內的程式碼。

C# 中建構函式的工作

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);
}
}

輸出:

C# 中的建構函數

上述程序的圖示:

C# 中的建構函數

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# 中的建構函數

上述程序的圖示:

C# 中的建構函數

C# 中最常見的 5 種建構子

C#提供了五種類型的建構子。它們如下:

1.預設建構子

  • 不帶任何參數的建構子稱為預設建構子。如果我們不明確定義它,那麼它將由編譯器隱式提供。
  • 在這種情況下,我們可以稱之為隱式建構子。預設或隱式建構函數使用預設值初始化類別的所有資料成員,例如所有數字欄位為零,所有字串和物件欄位為 null。

範例:

代碼:

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);
}
}

輸出:

C# 中的建構函數

2.參數化建構子

具有至少一個參數的建構子稱為參數化建構子。建立類別的實例時可以將參數傳遞給建構函式。它允許我們用不同的值初始化類別的每個實例。

範例:

代碼:

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);
}
}

輸出:

C# 中的建構函數

3.複製建構子

它是一個參數化建構函數,以同一個類別的物件作為參數。它將現有物件的值(作為參數傳遞)複製到建構函式實例化的新建立的物件。我們可以說它將一個物件的資料複製到另一個物件。

範例:

代碼:

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);
}
}

輸出:

C# 中的建構函數

4.靜態建構子

  • 可以透過在建構函式名稱前加上關鍵字來定義。如果類別包含任何靜態變量,則它由編譯器隱式定義(如果未明確定義)。
  • 它是類別中第一個執行的區塊,將自動呼叫。無論類別實例的數量有多少,它都只會執行一次。它是無參數的,並且不接受任何存取修飾符。

範例:

代碼:

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();
}
}

輸出:

C# 中的建構函數

5. Private Constructor

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:

C# 中的建構函數

Conclusion

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.

Recommended Article

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 –

  1. Constructor in JavaScript
  2. Constructor in C++
  3. Copy Constructor in C#
  4. Static Constructor in C#

以上是C# 中的建構函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 多維數組下一篇:C# 多維數組