.NET幫助 C# 虛擬關鍵字(對於開發者的運行原理) Jacob Mellor 更新:7月 28, 2025 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在 C# 中,虛擬關鍵字是物件導向程式設計中一個舉足輕重的概念,可促進多態性,讓開發人員在衍生類別中覆蓋方法。 這個關鍵字應用在類別方法、屬性或事件時,表示該實體可以由使用覆蓋關鍵字的派生類別修改其行為。 在本教程中,我們將學習 C# 虛擬關鍵字,並探索 IronPDF 函式庫。 讓我們直接了解其運作方式,並透過實例來瞭解其實際運作。 執行中的虛擬方法 虛擬關鍵字的基本用法 虛擬方法的核心是基類方法,虛擬方法允許派生類為基類中已定義的方法提供特定的實作。 C# 中的虛擬關鍵字將一個方法、屬性或事件標記為虛擬,表示可以在繼承該方法、屬性或事件的任何類別中覆寫該方法、屬性或事件。 考慮以下範例,我們定義了一個基類 Shape,其中有一個虛擬方法 Area: public class Shape { public virtual double Area() { return 0; // Default implementation, returns 0 } } public class Shape { public virtual double Area() { return 0; // Default implementation, returns 0 } } Public Class Shape Public Overridable Function Area() As Double Return 0 ' Default implementation, returns 0 End Function End Class $vbLabelText $csharpLabel 覆寫虛擬方法 派生類可以覆寫這些虛擬方法,針對派生類的特定需求提供自己的實作。 使用覆蓋關鍵字,讓我們建立一個 Circle 類別,它派生自 Shape 並提供自己版本的 Area 方法: public class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { Radius = radius; } public override double Area() { // Own implementation for circle area return Math.PI * Radius * Radius; } } public class Circle : Shape { public double Radius { get; set; } public Circle(double radius) { Radius = radius; } public override double Area() { // Own implementation for circle area return Math.PI * Radius * Radius; } } Public Class Circle Inherits Shape Public Property Radius() As Double Public Sub New(ByVal radius As Double) Me.Radius = radius End Sub Public Overrides Function Area() As Double ' Own implementation for circle area Return Math.PI * Radius * Radius End Function End Class $vbLabelText $csharpLabel 在上述程式碼中,Circle 類提供其 Area 方法的特定實作,該方法用來計算圓的面積。 這展示了虛擬方法在多態性中的威力。 非虛擬方法 值得注意的是,並非所有方法都需要或應該是虛擬的。 非虛擬方法的定義方式使其無法在派生類中被覆蓋,這意味著初始實作保持不變,並被所有繼承自該方法的類別所利用。 當基底類別提供了標準的實作而不應變更時,這將非常有用。 實際應用 讓我們將這些概念應用在實際情境中。 考慮以下使用我們的 Shape 和 Circle 類別的程式: public class Program { public static void Main(string[] args) { Shape myShape = new Shape(); Shape myCircle = new Circle(5); // Display the area calculation of the default and overridden methods. Console.WriteLine($"Shape area: {myShape.Area()}"); Console.WriteLine($"Circle area: {myCircle.Area()}"); } } public class Program { public static void Main(string[] args) { Shape myShape = new Shape(); Shape myCircle = new Circle(5); // Display the area calculation of the default and overridden methods. Console.WriteLine($"Shape area: {myShape.Area()}"); Console.WriteLine($"Circle area: {myCircle.Area()}"); } } Public Class Program Public Shared Sub Main(ByVal args() As String) Dim myShape As New Shape() Dim myCircle As Shape = New Circle(5) ' Display the area calculation of the default and overridden methods. Console.WriteLine($"Shape area: {myShape.Area()}") Console.WriteLine($"Circle area: {myCircle.Area()}") End Sub End Class $vbLabelText $csharpLabel 上面的範例程式展示了多態性的運作以及虛擬函數的精髓。 儘管 myCircle 宣告為 Shape 物件,它仍呼叫 Circle 類別中覆蓋的 Area 方法,展示了虛擬與覆蓋關鍵字所促成的動態分派機制。 。 虛擬和覆寫關鍵字的進階用法 抽象方法與類別 抽象方法更進一步,用於抽象類中。 抽象方法是在基類中宣告而沒有實作的方法,必須在派生類中覆寫。 它強制派生類提供抽象方法的實作,確保一致的介面,同時允許在每個派生類中自訂行為。 方法重載 vs. 重覆。 理解方法重載和方法覆寫之間的區別也是至關重要的。 方法重載發生在同一個類中,允許一個以上的方法具有相同的名稱但不同的參數。 透過虛擬和覆蓋關鍵字,方法覆蓋允許派生類為基類中定義的方法提供不同的實作。 虛擬屬性與事件 除了方法之外,屬性和事件也可以是虛擬的。 這可讓衍生類別提供自訂的 getter、setter 及事件處理程式,進一步增強類別階層的靈活性。 IronPDF:.NET PDF 函式庫 IronPDF 是專為 C# 開發人員設計的綜合資料庫,可直接在 .NET 應用程式中產生、處理和渲染 PDF 文件。 它提供直觀的 API,可簡化 處理 PDF 檔案的工作,例如使用 HTML 來建立 PDF,協助開發人員建立、編輯和轉換 PDF,而無需瞭解複雜的底層 PDF 文件結構或借助外部軟體。 IronPdf 可與語言的物件導向功能(包括虛擬關鍵字的使用)無縫整合,提供可客製化的 PDF 處理功能。 將虛擬關鍵字與 IronPDF 搭配使用,可讓開發人員在其應用程式中擴充 IronPDF 類別的功能。 透過定義具有 PDF 產生或處理相關虛擬方法的基類,開發人員可以建立覆寫這些方法的派生類,以根據特定需求量身打造 PDF 處理行為。 範例:使用虛擬方法自訂 PDF 渲染。 假設您有一個使用 IronPDF 來從 HTML 字串渲染 PDF 的基本類別。 透過將渲染方法標示為虛擬,您可以允許派生類修改或增強渲染過程。 下面是一個簡單的例子: public class BasicPdfRenderer { // Virtual method allowing customization in derived classes public virtual byte[] RenderHtmlToPdf(string htmlContent) { // Use IronPDF to render PDF from HTML var renderer = new IronPdf.ChromePdfRenderer(); var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); return pdfDocument.BinaryData; } } public class CustomPdfRenderer : BasicPdfRenderer { // Overriding the base class method to implement custom rendering settings public override byte[] RenderHtmlToPdf(string htmlContent) { var renderer = new IronPdf.ChromePdfRenderer(); var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); // Apply a prominent watermark to the PDF document pdfDocument.ApplyWatermark("<h2 style='color:red; font-size: 60px; opacity: 0.5; text-shadow: 2px 2px 5px grey;'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center); // Return the binary data of the PDF document return pdfDocument.BinaryData; } } class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; // HTML content to be converted to PDF string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a simple PDF document generated from HTML.</p>"; // Create an instance of CustomPdfRenderer CustomPdfRenderer renderer = new CustomPdfRenderer(); // Call RenderHtmlToPdf method to generate PDF binary data byte[] pdfData = renderer.RenderHtmlToPdf(htmlContent); // Specify the file path to save the PDF string filePath = "f:\\CustomRenderedPdf.pdf"; // Save the binary data to a file File.WriteAllBytes(filePath, pdfData); // Output success message Console.WriteLine($"PDF generated and saved to {filePath}"); } } public class BasicPdfRenderer { // Virtual method allowing customization in derived classes public virtual byte[] RenderHtmlToPdf(string htmlContent) { // Use IronPDF to render PDF from HTML var renderer = new IronPdf.ChromePdfRenderer(); var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); return pdfDocument.BinaryData; } } public class CustomPdfRenderer : BasicPdfRenderer { // Overriding the base class method to implement custom rendering settings public override byte[] RenderHtmlToPdf(string htmlContent) { var renderer = new IronPdf.ChromePdfRenderer(); var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent); // Apply a prominent watermark to the PDF document pdfDocument.ApplyWatermark("<h2 style='color:red; font-size: 60px; opacity: 0.5; text-shadow: 2px 2px 5px grey;'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center); // Return the binary data of the PDF document return pdfDocument.BinaryData; } } class Program { static void Main(string[] args) { License.LicenseKey = "License-Key"; // HTML content to be converted to PDF string htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a simple PDF document generated from HTML.</p>"; // Create an instance of CustomPdfRenderer CustomPdfRenderer renderer = new CustomPdfRenderer(); // Call RenderHtmlToPdf method to generate PDF binary data byte[] pdfData = renderer.RenderHtmlToPdf(htmlContent); // Specify the file path to save the PDF string filePath = "f:\\CustomRenderedPdf.pdf"; // Save the binary data to a file File.WriteAllBytes(filePath, pdfData); // Output success message Console.WriteLine($"PDF generated and saved to {filePath}"); } } Public Class BasicPdfRenderer ' Virtual method allowing customization in derived classes Public Overridable Function RenderHtmlToPdf(ByVal htmlContent As String) As Byte() ' Use IronPDF to render PDF from HTML Dim renderer = New IronPdf.ChromePdfRenderer() Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent) Return pdfDocument.BinaryData End Function End Class Public Class CustomPdfRenderer Inherits BasicPdfRenderer ' Overriding the base class method to implement custom rendering settings Public Overrides Function RenderHtmlToPdf(ByVal htmlContent As String) As Byte() Dim renderer = New IronPdf.ChromePdfRenderer() Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Apply a prominent watermark to the PDF document pdfDocument.ApplyWatermark("<h2 style='color:red; font-size: 60px; opacity: 0.5; text-shadow: 2px 2px 5px grey;'>SAMPLE</h2>", 30, IronPdf.Editing.VerticalAlignment.Middle, IronPdf.Editing.HorizontalAlignment.Center) ' Return the binary data of the PDF document Return pdfDocument.BinaryData End Function End Class Friend Class Program Shared Sub Main(ByVal args() As String) License.LicenseKey = "License-Key" ' HTML content to be converted to PDF Dim htmlContent As String = "<h1>Hello, IronPDF!</h1><p>This is a simple PDF document generated from HTML.</p>" ' Create an instance of CustomPdfRenderer Dim renderer As New CustomPdfRenderer() ' Call RenderHtmlToPdf method to generate PDF binary data Dim pdfData() As Byte = renderer.RenderHtmlToPdf(htmlContent) ' Specify the file path to save the PDF Dim filePath As String = "f:\CustomRenderedPdf.pdf" ' Save the binary data to a file File.WriteAllBytes(filePath, pdfData) ' Output success message Console.WriteLine($"PDF generated and saved to {filePath}") End Sub End Class $vbLabelText $csharpLabel 我們在 BasicPdfRenderer 類中使用 IronPDF 來將 HTML 轉換為 PDF,並將其 RenderHtmlToPdf 方法標記為虛擬,以允許客製化。 CustomPdfRenderer 類別源自 BasicPdfRenderer,它會覆寫此方法,不僅會執行轉換,還會在生成的 PDF 中注入明顯、大的紅色 水印。 輸出 PDF 檔案 這是 IronPDF 所產生的 PDF 檔案: 。 結論 。 C# 中的虛擬關鍵字是物件導向程式設計的基石,可實現多態性和動態分派。 透過允許派生類提供基類中定義的方法、屬性和事件的特定實作,它賦予開發人員創造靈活和可重用程式碼結構的能力。 透過實例以及瞭解虛擬方法、覆蓋機制和類層級之間的關係,開發人員可以有效地利用這些概念建立強大的應用程式。 此外,這些概念也將有助於開發人員在其應用程式中更有效率地使用 IronPDF。 您可以使用 IronPDF 的 免費試用選項,在不花費任何費用的情況下測試 IronPDF。 常見問題解答 如何在 C# 中使用虛擬方法自訂 PDF 渲染? 您可以透過將基類方法 (例如渲染函數) 標示為虛擬來自訂 PDF 渲染。這允許派生類覆寫該方法並修改渲染過程,例如,使用 IronPDF 添加水印或變更渲染設置。 虛擬關鍵字在 PDF 文件處理中扮演什麼角色? 虛擬關鍵字允許開發人員為 PDF 文件處理建立彈性且可重用的程式碼架構。通過使用虛擬方法,開發人員可以在 IronPDF 的幫助下,擴展和定制功能,例如修改 PDF 渲染,以滿足特定應用程式的需求。 覆蓋機制如何增強 C# 中的 PDF 生成功能? 覆蓋機制允許派生類為基類中標記為虛擬的方法提供特定的實作。這在 PDF 生成中特別有用,因為開發人員可以覆寫方法來自訂 PDF 的建立,例如使用 IronPDF 來改變版面或加入額外的功能。 虛擬方法可以提高 PDF 處理應用程式的靈活性嗎? 是的,虛擬方法可以大幅提升 PDF 處理應用程式的靈活性。它們允許開發人員創建具有可定制行為的基類,讓派生類修改或擴展 PDF 處理功能,從而充分發揮 IronPDF 等庫的潛力。 虛擬方法和非虛擬方法在 PDF 操作方面有何不同? 虛擬方法可以在派生類中覆寫,以提供自訂行為,如果需要擴充或修改特定功能,這對 PDF 的操作非常有利。另一方面,非虛擬方法則無法覆寫,以確保所有派生類的行為一致。 在使用 C# 處理 PDF 時,多態性的意義何在? 多態性(Polymorphism)由虛擬關鍵字(virtual keyword)促成,允許基於執行時物件類型的動態方法調用。這在 PDF 處理中非常重要,因為它能讓開發人員實作彈性、適應性強的程式碼,使用 IronPDF 等工具有效率地處理各種 PDF 操作任務。 開發人員如何測試 C# 應用程式中的 PDF 處理功能? 開發人員可以利用 IronPDF 等 PDF 函式庫的免費試用版來測試 C# 應用程式中的 PDF 處理功能。這些試用版可讓他們探索功能、嘗試使用程式碼,並評估其應用程式中 PDF 處理功能的整合。 Jacob Mellor 立即與工程團隊聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 相關文章 更新12月 11, 2025 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多 更新9月 4, 2025 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新9月 4, 2025 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 C# 泛型(對於開發者的運行原理)NativeUI C#(對於開發者的運...
更新12月 11, 2025 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多