C# 中的部分

王林
發布: 2024-09-03 15:31:23
原創
318 人瀏覽過

C# 的一個特殊功能是分部類,使用分部類可以將單一類別功能實現到多個文件中,然後在應用程式編譯期間將這些文件組合在一起形成單一類文件,並且使用partial 關鍵字在C# 中建立分部類,此分部關鍵字也用於將方法功能、介面功能或結構功能劃分為多個文件,並且所有這些文件在強制編譯期間必須可用,以建立最終文件和使用者還可以使用嵌套的部分類型。

文法:

public partial Class_name
{
// code
}
登入後複製

C# 中的部分類別的函數

必須在任何類別名稱之前使用partial關鍵字才能使該類別成為分部類別。可以使用partial關鍵字將介面、結構或類別分為多個部分。透過將類別作為分部類,可以將單一類別分為多個文件。在部分程式碼的編譯過程中,多個類別或多個介面或結構被編譯為單一類別或單一介面或結構。可以使用partial關鍵字將用於設計的使用者介面程式碼與用於業務邏輯的程式碼分開,這使得工作和理解更容易。

使用分部類,多位開發人員可以並行工作。客製化的邏輯程式碼可以透過使用部分類別嵌入到框架自動產生的程式碼中。透過將較大的類別劃分為較小的類,可以輕鬆地理解和維護較大的類別。透過將介面劃分為多個可以與多個開發人員共享的程式碼,可以加快應用程式的開發速度。密封部分類別會導致整個類別被密封。這稱為分部類的密封屬性。將部分類別抽象化會導致整個類別成為抽象類別。這稱為分部類別的抽象屬性。具有相同名稱的分部類別必須僅在同一命名空間範圍內聲明。

在 C# 中實作 Partial 的範例

考慮以下範例來理解 C# 中分部類別的概念:

範例#1

 代碼:

using System
public class Check
{
//main method is called
public static void Main()
{
//the same partuial class is defined at two places twice but during compilation it is executed as a single file
parclass pc=new parclass();
pc.firstmethod();
pc.secmethod();
}
//partial class is defined with the same class name
public partial class parclass
{
//a method is declared
public void firstmethod()
{
Console.WriteLine("The first method is called");
}
}
//another partial class is defined with the same name
public partial class parclass
{
//another method is declared
public void secmethod()
{
Console.WriteLine("The second method is called");
}
}
}
登入後複製

輸出:

C# 中的部分

說明:在上面的程式中,定義了一個名為 check 的類,在該類別中呼叫了 main 方法。此主方法由稍後定義的分部類別的實例組成,使用該實例來呼叫分部類別的方法。定義了兩個具有相同名稱 parclass 的分部類別。它們包含在主方法中呼叫的不同方法。分部類別在編譯過程中將多個類別合併為一個類別,輸出如上面的快照所示。

範例#2

C# 程式示範分部類,同時為變數賦值並使用兩個不同的類別列印它們。

 代碼:

using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during compilation it is executed as a single file
rec r=new rec(5,10);
r.print();
Console.ReadLine();
}
//partial class is defined with the same class name
public partial class rec
{
private int a;
private int b;
//a method is declared
public rec(int a, int b)
{
this.a = a;
this.b = b;
}
}
//another partial class is defined with the same name
public partial class rec
{
//another method is declared
public void print()
{
Console.WriteLine("The value of a is "+ a);
Console.WriteLine("The value of b is "+ b);
}
}
}
登入後複製

輸出:

C# 中的部分

說明:在上面的程式中,定義了一個名為 check 的類,在該類別中呼叫了 main 方法。此主方法由稍後定義的分部類別的實例組成,使用該實例來呼叫分部類別的方法。定義了兩個同名的分部類別。它們包含在主方法中呼叫的不同方法。分部類別在編譯過程中將多個類別合併為一個類別,輸出如上面的快照所示。

範例 #3

C# 程式示範分部類,同時為變數賦值並使用兩個不同的類別列印它們。

代碼:

using System;
public class Check
{
//main method is called
public static void Main()
{
//the same partial class is defined at two places twice but during      compilation it is executed as a single file
stat r=new stat();
r.print1();
r.print2();
}
//partial class is defined with the same class name
public partial class stat
{
public void print1()
{
Console.WriteLine("Hello, welcome to Partial class one");
}
}
//another partial class is defined with the same name
public partial class stat
{
//another method is declared
public void print2()
{
Console.WriteLine("Hello, welcome to partial class two");
}
}
} <strong>Output:</strong>
登入後複製

C# 中的部分

說明:在上面的程式中,定義了一個名為 check 的類,在該類別中呼叫了 main 方法。此主方法由稍後定義的分部類別的實例組成,使用該實例來呼叫分部類別的方法。定義了兩個具有相同名稱 stat 的分部類別。它們包含在主方法中呼叫的不同方法。分部類別在編譯過程中將多個類別合併為一個類別,輸出如上面的快照所示。

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!