.NET幫助 C# 面向對象(對於開發者的運行原理) Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 物件導向程式設計(OOP)是軟體開發中的基本概念,使程式設計師能夠創建模組化、可重用和可適應的程式碼。 C# 這種現代的物件導向編程語言,提供了一個用於構建複雜應用程式的強大框架。 本指南使用 C# 介紹 OOP 概念,著重於實際實施和編碼範例,以幫助初學者有效地理解和應用這些原則。 我們還將討論如何在 C# 中應用這些原則與 IronPDF 庫。 理解物件導向程式設計概念 OOP 的核心有幾個關鍵概念:類別、物件、繼承、多型、抽象和封裝。 這些概念使開發人員能夠建模現實世界的實體,管理複雜性並提高代碼的可維護性。 類別與物件:構建模塊 類別創建個別物件。 類別是定義類別物件共享的資料和行為的藍圖。 物件是類別的具象化。 它體現了真實的值,而不是類別藍圖中定義的抽象佔位符。 public class Car // A class declared as 'Car' defines its structure and behavior. { public string Name; public string Color; public void DisplayInfo() { Console.WriteLine($"Name: {Name}, Color: {Color}"); } } class Program // This is the program class, serving as the entry point of a C# program. { static void Main(string[] args) { Car myCar = new Car(); myCar.Name = "Toyota"; myCar.Color = "Red"; myCar.DisplayInfo(); } } public class Car // A class declared as 'Car' defines its structure and behavior. { public string Name; public string Color; public void DisplayInfo() { Console.WriteLine($"Name: {Name}, Color: {Color}"); } } class Program // This is the program class, serving as the entry point of a C# program. { static void Main(string[] args) { Car myCar = new Car(); myCar.Name = "Toyota"; myCar.Color = "Red"; myCar.DisplayInfo(); } } Public Class Car ' A class declared as 'Car' defines its structure and behavior. Public Name As String Public Color As String Public Sub DisplayInfo() Console.WriteLine($"Name: {Name}, Color: {Color}") End Sub End Class Friend Class Program ' This is the program class, serving as the entry point of a C# program. Shared Sub Main(ByVal args() As String) Dim myCar As New Car() myCar.Name = "Toyota" myCar.Color = "Red" myCar.DisplayInfo() End Sub End Class $vbLabelText $csharpLabel 在此示例中,Car 類別有兩個數據成員(Name 和 Color)及一個方法(DisplayInfo)。 Main 方法作為應用程序的入口點,創建 Car 類別的實例,將值賦給其欄位,然後調用其方法來顯示這些值。 繼承:擴展現有類別 繼承允許類別繼承現有類別的屬性和方法。 屬性被繼承的類別稱為基類,而繼承這些屬性的類別稱為派生類。 public class Vehicle { public string LicensePlate; public void Honk() { Console.WriteLine("Honking"); } } public class Truck : Vehicle // Truck is a child class derived from the Vehicle base class. { public int CargoCapacity; } class Program { static void Main(string[] args) { Truck myTruck = new Truck(); myTruck.LicensePlate = "ABC123"; myTruck.CargoCapacity = 5000; myTruck.Honk(); } } public class Vehicle { public string LicensePlate; public void Honk() { Console.WriteLine("Honking"); } } public class Truck : Vehicle // Truck is a child class derived from the Vehicle base class. { public int CargoCapacity; } class Program { static void Main(string[] args) { Truck myTruck = new Truck(); myTruck.LicensePlate = "ABC123"; myTruck.CargoCapacity = 5000; myTruck.Honk(); } } Public Class Vehicle Public LicensePlate As String Public Sub Honk() Console.WriteLine("Honking") End Sub End Class Public Class Truck ' Truck is a child class derived from the Vehicle base class. Inherits Vehicle Public CargoCapacity As Integer End Class Friend Class Program Shared Sub Main(ByVal args() As String) Dim myTruck As New Truck() myTruck.LicensePlate = "ABC123" myTruck.CargoCapacity = 5000 myTruck.Honk() End Sub End Class $vbLabelText $csharpLabel 在此示例中,Truck 是派生類,擴展了 Vehicle 基類,繼承其 LicensePlate 字段和 Honk 方法,同時添加了一個新字段,CargoCapacity。 多型與抽象:介面與抽象類別 多型使物件可以作為其基類的實例處理,而不是作為其具體類別。 抽象允許您定義無法實例化的抽象類別和介面,但可以用作基類。 抽象類別與方法 抽象類別不能被實例化,通常用於提供基類的公共定義,以便多個派生類可以共享。 public abstract class Shape { public abstract void Draw(); } public class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle"); } } public abstract class Shape { public abstract void Draw(); } public class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle"); } } Public MustInherit Class Shape Public MustOverride Sub Draw() End Class Public Class Circle Inherits Shape Public Overrides Sub Draw() Console.WriteLine("Drawing a circle") End Sub End Class $vbLabelText $csharpLabel 實現多個介面 介面建立一種協議,或合同,類別可以通過實現其定義的方法來履行。 類別可以實現多個介面,這是一種多型形式。 public interface IDrawable { void Draw(); } public interface IColorable { void Color(); } public class CustomShape : IDrawable, IColorable // Defining a new class CustomShape that implements IDrawable and IColorable. { public void Draw() { Console.WriteLine("Custom shape drawn"); } public void Color() { Console.WriteLine("Custom shape colored"); } } public interface IDrawable { void Draw(); } public interface IColorable { void Color(); } public class CustomShape : IDrawable, IColorable // Defining a new class CustomShape that implements IDrawable and IColorable. { public void Draw() { Console.WriteLine("Custom shape drawn"); } public void Color() { Console.WriteLine("Custom shape colored"); } } Public Interface IDrawable Sub Draw() End Interface Public Interface IColorable Sub Color() End Interface Public Class CustomShape ' Defining a new class CustomShape that implements IDrawable and IColorable. Implements IDrawable, IColorable Public Sub Draw() Implements IDrawable.Draw Console.WriteLine("Custom shape drawn") End Sub Public Sub Color() Implements IColorable.Color Console.WriteLine("Custom shape colored") End Sub End Class $vbLabelText $csharpLabel 封裝:保護數據 封裝是一種限制對物件某些組件的訪問,並防止外部看到內部表示的機制。 這是通過使用訪問修飾符來實現的。 public class Person { private string name; // Private variable, inaccessible outside the class public string Name // Public property to access the private variable { get { return name; } set { name = value; } } } // Example showing a simple customer class with encapsulated data public class Customer { public string Name { get; set; } public string Address { get; set; } } public class Person { private string name; // Private variable, inaccessible outside the class public string Name // Public property to access the private variable { get { return name; } set { name = value; } } } // Example showing a simple customer class with encapsulated data public class Customer { public string Name { get; set; } public string Address { get; set; } } Public Class Person 'INSTANT VB NOTE: The field name was renamed since Visual Basic does not allow fields to have the same name as other class members: Private name_Conflict As String ' Private variable, inaccessible outside the class Public Property Name() As String ' Public property to access the private variable Get Return name_Conflict End Get Set(ByVal value As String) name_Conflict = value End Set End Property End Class ' Example showing a simple customer class with encapsulated data Public Class Customer Public Property Name() As String Public Property Address() As String End Class $vbLabelText $csharpLabel 在此示例中,name 字段是私有的,無法在 Person 類別外訪問。 訪問此字段是通過公共 Name 屬性提供的,其中包括 get 和 set 方法。 實用用例與編碼示例 現在,我們將探索一個涉及多個類別的示例,以展示這些原則的實際應用。 using System; namespace OOPExample { public class Program { static void Main(string[] args) { ElectricCar myElectricCar = new ElectricCar(); myElectricCar.Make = "Tesla"; myElectricCar.Model = "Model 3"; myElectricCar.BatteryLevel = 100; myElectricCar.Drive(); myElectricCar.Charge(); } } public abstract class Vehicle { public string Make { get; set; } public string Model { get; set; } public abstract void Drive(); } public class Car : Vehicle { public override void Drive() { Console.WriteLine($"The {Make} {Model} is driving."); } } public class ElectricCar : Car { public int BatteryLevel { get; set; } public void Charge() { Console.WriteLine("Charging the car."); } public override void Drive() { Console.WriteLine($"The {Make} {Model} is driving silently."); } } } using System; namespace OOPExample { public class Program { static void Main(string[] args) { ElectricCar myElectricCar = new ElectricCar(); myElectricCar.Make = "Tesla"; myElectricCar.Model = "Model 3"; myElectricCar.BatteryLevel = 100; myElectricCar.Drive(); myElectricCar.Charge(); } } public abstract class Vehicle { public string Make { get; set; } public string Model { get; set; } public abstract void Drive(); } public class Car : Vehicle { public override void Drive() { Console.WriteLine($"The {Make} {Model} is driving."); } } public class ElectricCar : Car { public int BatteryLevel { get; set; } public void Charge() { Console.WriteLine("Charging the car."); } public override void Drive() { Console.WriteLine($"The {Make} {Model} is driving silently."); } } } Imports System Namespace OOPExample Public Class Program Shared Sub Main(ByVal args() As String) Dim myElectricCar As New ElectricCar() myElectricCar.Make = "Tesla" myElectricCar.Model = "Model 3" myElectricCar.BatteryLevel = 100 myElectricCar.Drive() myElectricCar.Charge() End Sub End Class Public MustInherit Class Vehicle Public Property Make() As String Public Property Model() As String Public MustOverride Sub Drive() End Class Public Class Car Inherits Vehicle Public Overrides Sub Drive() Console.WriteLine($"The {Make} {Model} is driving.") End Sub End Class Public Class ElectricCar Inherits Car Public Property BatteryLevel() As Integer Public Sub Charge() Console.WriteLine("Charging the car.") End Sub Public Overrides Sub Drive() Console.WriteLine($"The {Make} {Model} is driving silently.") End Sub End Class End Namespace $vbLabelText $csharpLabel 在這個例子中,Drive() 是從 Vehicle 抽象類別來的抽象方法。 Car 是一個實現 Drive() 的派生類,而 ElectricCar 是在層次結構中下一層,加入了 BatteryLevel 等新功能及其自己的 Drive() 實現。 這個結構展示了抽象、封裝、繼承和多型在 C# 應用程序中的協同運作。 IronPDF:C# PDF 庫 IronPDF 庫適用於 .NET 是一個多功能工具,為 C# 開發者設計,以簡化在 .NET 應用程序中創建、編輯和提取 PDF 文檔的過程。 IronPDF 能夠輕鬆地從 HTML 字符串、URL 或 ASPX 文件生成 PDF,提供對 PDF 創建和操作過程的高度控制。 此外,IronPDF 支持添加頁眉和頁腳、水印和加密等高級功能,成為在 .NET 應用中處理 PDF 的綜合解決方案。 利用 IronPDF 的 OOP 示例 這是一個簡化的示例,展示了在 C# 應用程序中使用 IronPDF,結合 virtual 關鍵字來示範如何通過繼承來擴展 IronPDF 的功能,這是 OOP 核心概念之一。假設我們有一個生成基本 PDF 報告的基礎類,和一個擴展此功能以包括自訂標頭的衍生類: using IronPdf; public class BasicReportGenerator { public virtual PdfDocument GenerateReport(string htmlContent) { var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf(htmlContent); return pdf; } } public class CustomReportGenerator : BasicReportGenerator { public override PdfDocument GenerateReport(string htmlContent) { var pdf = base.GenerateReport(htmlContent); AddCustomHeader(pdf, "Custom Report Header"); return pdf; } private void AddCustomHeader(PdfDocument document, string headerContent) { // Create text header TextHeaderFooter textHeader = new TextHeaderFooter { CenterText = headerContent, }; document.AddTextHeaders(textHeader); } } class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; // HTML content for the report string htmlContent = "<html><body><h1>Sample Report</h1><p>This is a sample report content.</p></body></html>"; // Using BasicReportGenerator BasicReportGenerator basicReportGenerator = new BasicReportGenerator(); var basicPdf = basicReportGenerator.GenerateReport(htmlContent); basicPdf.SaveAs("basic_report.pdf"); // Using CustomReportGenerator CustomReportGenerator customReportGenerator = new CustomReportGenerator(); var customPdf = customReportGenerator.GenerateReport(htmlContent); customPdf.SaveAs("custom_report.pdf"); Console.WriteLine("PDF reports generated successfully."); } } using IronPdf; public class BasicReportGenerator { public virtual PdfDocument GenerateReport(string htmlContent) { var renderer = new ChromePdfRenderer(); var pdf = renderer.RenderHtmlAsPdf(htmlContent); return pdf; } } public class CustomReportGenerator : BasicReportGenerator { public override PdfDocument GenerateReport(string htmlContent) { var pdf = base.GenerateReport(htmlContent); AddCustomHeader(pdf, "Custom Report Header"); return pdf; } private void AddCustomHeader(PdfDocument document, string headerContent) { // Create text header TextHeaderFooter textHeader = new TextHeaderFooter { CenterText = headerContent, }; document.AddTextHeaders(textHeader); } } class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; // HTML content for the report string htmlContent = "<html><body><h1>Sample Report</h1><p>This is a sample report content.</p></body></html>"; // Using BasicReportGenerator BasicReportGenerator basicReportGenerator = new BasicReportGenerator(); var basicPdf = basicReportGenerator.GenerateReport(htmlContent); basicPdf.SaveAs("basic_report.pdf"); // Using CustomReportGenerator CustomReportGenerator customReportGenerator = new CustomReportGenerator(); var customPdf = customReportGenerator.GenerateReport(htmlContent); customPdf.SaveAs("custom_report.pdf"); Console.WriteLine("PDF reports generated successfully."); } } Imports IronPdf Public Class BasicReportGenerator Public Overridable Function GenerateReport(ByVal htmlContent As String) As PdfDocument Dim renderer = New ChromePdfRenderer() Dim pdf = renderer.RenderHtmlAsPdf(htmlContent) Return pdf End Function End Class Public Class CustomReportGenerator Inherits BasicReportGenerator Public Overrides Function GenerateReport(ByVal htmlContent As String) As PdfDocument Dim pdf = MyBase.GenerateReport(htmlContent) AddCustomHeader(pdf, "Custom Report Header") Return pdf End Function Private Sub AddCustomHeader(ByVal document As PdfDocument, ByVal headerContent As String) ' Create text header Dim textHeader As New TextHeaderFooter With {.CenterText = headerContent} document.AddTextHeaders(textHeader) End Sub End Class Friend Class Program Shared Sub Main(ByVal args() As String) License.LicenseKey = "License-Key" ' HTML content for the report Dim htmlContent As String = "<html><body><h1>Sample Report</h1><p>This is a sample report content.</p></body></html>" ' Using BasicReportGenerator Dim basicReportGenerator As New BasicReportGenerator() Dim basicPdf = basicReportGenerator.GenerateReport(htmlContent) basicPdf.SaveAs("basic_report.pdf") ' Using CustomReportGenerator Dim customReportGenerator As New CustomReportGenerator() Dim customPdf = customReportGenerator.GenerateReport(htmlContent) customPdf.SaveAs("custom_report.pdf") Console.WriteLine("PDF reports generated successfully.") End Sub End Class $vbLabelText $csharpLabel 在此示例中,BasicReportGenerator 有一個方法 GenerateReport,它接收 HTML 內容,並使用 IronPDF 生成 PDF 文檔。 CustomReportGenerator 類別,繼承自 BasicReportGenerator,覆蓋 GenerateReport 方法,以在基礎方法生成 PDF 後添加自訂標頭。 以下是代碼生成的自訂報告: 結論 通過理解和應用 OOP 的基本原則,初學者可以在掌握 C# 和開發健壯的軟體解決方案方面邁出重要一步。 繼承和多型允許代碼復用和靈活性,使新類別能夠在現有結構和功能的基礎上進行構建。 抽象和封裝確保類別僅向外界暴露必要的內容,保持內部工作私密,防止意外使用。 您可以嘗試 IronPDF C# 中的 PDF 生成免費試用,從 liteLicense 開始提供。 常見問題解答 如何在 C# 中應用面向物件程式設計原則? 在 C# 中,您可以透過定義類別和物件來模擬現實世界中的實體以應用面向物件程式設計原則。 使用繼承來擴展類別,使用多型來允許方法覆寫,並使用封裝來保護資料,從而創建模組化和可維護的代碼。 抽象在 C# 程式設計中扮演什麼角色? C# 中的抽象允許開發人員簡化複雜系統,通過明確區分抽象的高階概念和具體實現。 抽象類別和介面用於為其他類別定義藍圖,確保應用程式不同部分的結構和行為一致。 如何在 C# 中使用面向物件原則創建 PDF 報告? 您可以使用 IronPDF 庫在 C# 中創建 PDF 報告,該庫允許您從 HTML 內容生成 PDF 文件。 通過運用面向物件原則,您可以為報告生成創建基類,並透過派生類添加特定功能,例如添加自訂的標頭或頁尾。 在 C# 中使用封裝有什麼好處? C# 中的封裝提供了透過限制對物件內部元件的存取來保護物件資料的優勢。 這是使用存取修飾符實現的,有助於維護資料的完整性並防止程式的其他部分進行未預期的干擾。 如何在 C# 應用程式中通過多型來實現? 在 C# 中,可以透過在派生類別中使用方法覆寫來實現多型。 這允許您在基類中定義方法並在派生類別中覆寫方法,提供靈活性,並允許物件被視為它們基類的實例。 如何擴展 C# 中的 PDF 庫的功能? 您可以透過創建自訂類別來擴展像 IronPDF 這樣的 PDF 庫的功能,這些類別實現了附加功能。 例如,您可以創建一個從基類 PDF 生成類繼承的類別,並添加方法以自訂 PDF 頁面的外觀或內容。 您能否提供一個使用 C# 繼承的程式碼示例? C# 繼承的程式碼示例可能涉及定義一個基類 `Vehicle`,其包含 `Speed` 和 `Fuel` 等屬性。 派生類 `Car` 可以擴展 `Vehicle` 並添加如 `NumberOfDoors` 的具體功能。 這展示了派生類如何繼承和擴展基類的功能。 IronPDF 如何與 C# 中的面向物件程式設計集成? IronPDF 透過允許開發者創建封裝 PDF 生成邏輯的類別來與 C# 中的面向物件程式設計集成。 您可以為常見的 PDF 操作定義基類,並透過繼承和多型擴展它們的特定功能,如添加水印或自訂格式。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新日期 9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 更新日期 8月 5, 2025 C#開關模式匹配(對開發者來說是如何工作的) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 C# Action(對於開發者的運行原理)C# String.Join(對於開發者的...