首頁  >  文章  >  後端開發  >  C# 基礎

C# 基礎

WBOY
WBOY原創
2024-09-03 15:18:21401瀏覽

base關鍵字用來存取衍生類別中基底類別的建構子、欄位和方法。我們在 C# 中將此關鍵字稱為「base」。您只能在實例方法、實例屬性存取器或建構函式中使用此基本關鍵字。當基底類別和衍生類別都具有相同的字段,且衍生類別不會覆寫從基底類別繼承的字段時,base 關鍵字就變得有用。

文法

C# Base 關鍵字的語法如下:

base.constructor_name/field_name/method_name;

哪裡,

constructor_name 是基底類別中建構子的名稱,

field_name 是基底類別中欄位的名稱,

method_name 是基底類別中方法的名稱。

C# Base 關鍵字如何運作?

每當衍生類別中需要使用基底類別的建構子或欄位或方法時,我們在衍生類別中使用關鍵字base。

在Python中,程式設計師使用super()函數來引用基底類別並呼叫其方法或存取其屬性。它提供了一種在衍生類別中呼叫基底類別方法的方法。如果基底類別和衍生類別中都存在相同的字段,則 base 關鍵字很有用;如果派生類別沒有派生基底類別中存在的字段,則基底關鍵字沒有用處。

透過使用基底關鍵字,可以在衍生類別中消除必須從基底類別引用哪個成員的混亂。

實作 C# 基礎的範例

以下是提到的範例:

範例#1

C#程式示範如何使用base關鍵字在衍生類別中引用基底類別的變數:

代碼:

using System;
//a class called check is defined which is the base class
public class check
{
//a string variable is defined to store the string
public string string1 = "Welcome to";
}
//another class called check1 is defined which is derived from the base class called check
public class check1: check
{
//another string variable is defined to store the another string
string string2 = "C#";
//a method is defined inside the derived class which displays the string from the base class and the derived class as well
public void displaymsg()
{
Console.WriteLine(base.string1);
Console.WriteLine(string2);
}
}
//another class called check2 is defined within which the main method is called the instance of the derived class is created and the method of the derived class is called which in turn accesses the variable of the base class
public class check2
{
public static void Main()
{
check1 ob = new check1();
ob.displaymsg();
}
}

輸出:

C# 基礎

說明:上面的程式定義了一個名為「Check」的基底類,作為父類。它包括一個字串變數來儲存字串值。在給定的程式中,定義了一個名為「Check1」的衍生類,它繼承自基底類別「Check」。在「Check1」衍生類別中,宣告了另一個字串變數來儲存不同的字串值。此方法顯示來自基底類別和衍生類別的字串值。輸出如上面的快照所示。

範例#2

C#程式示範如何使用base關鍵字在衍生類別中引用基底類別的變數:

代碼:

using System;
//a class called check is defined which is the base class
public class check
{
//a string variable is defined to store the string
public string string1 = "Learning is";
}
//another class called check1 is defined which is derived from the base class called check
public class check1: check
{
//another string variable is defined to store the another string
string string2 = "Fun";
//a method is defined inside the derived class which displays the string from the base class and the derived class as well
public void displaymsg()
{
Console.WriteLine(base.string1);
Console.WriteLine(string2);
}
}
//another class called check2 is defined within which the main method is called the instance of the derived class is created and the method of the derived class is called which in turn accesses the variable of the base class
public class check2
{
public static void Main()
{
check1 ob = new check1();
ob.displaymsg();
}
}

輸出:

C# 基礎

說明:在給定的程式中,有一個名為「Check」的基底類,它充當其他類別的父類。它包括一個字串變數來保存字串值。此外,定義了一個名為「Check1」的衍生類,它繼承自基底類別「Check」。在「Check1」衍生類別中,宣告了一個附加字串變數來儲存不同的字串值。衍生類別「Check1」也包含方法定義。此方法顯示來自基底類別和衍生類別的字串值。輸出如上面的快照所示。

C# 基礎的優點

使用base關鍵字有幾個優點;他們是:

1.使用 base 關鍵字消除了重複程式碼的需要。

2.透過使用 base 關鍵字,可以在衍生類別中消除必須從基底類別引用哪個成員的混亂。

結論

在本教程中,我們透過程式設計範例、其輸出以及在程式中使用 base 關鍵字的優點,透過定義、語法和基本關鍵字的工作原理來了解 C# 中基本關鍵字的概念。

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

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