Interface,在C#中是一個關鍵字,它包含一組抽象方法和屬性,這些方法和屬性由抽象或非抽象類實現或使用。定義方法是接口內的屬性,默認情況下它們是公共和抽象的。
用最簡單的話來說,接口就像一個契約,主體中包含的每個成員或組件都必須遵循契約,它定義了必須做什么。該接口不包含任何字段,并且始終通過使用關鍵字“interface”來定義。
語法:
語法以接口關鍵字開頭,后跟接口名稱,然后是正文。
interface <name_for_interface> { //abstract methods //abstract properties. }
如您所見,我們有 C# 中接口的標準語法,它以“interface”關鍵字開頭,然后是接口的名稱,然后是主體內的抽象方法和屬性。在 C# 中,可以在類或結構內部實現和使用多個接口。這些接口可以將各種方法、索引器、屬性以及事件作為成員。
為什么我們需要 C# 接口?
基本上我們已經明白接口內部沒有特定的功能,如果是這樣,那我們?yōu)槭裁葱枰涌冢?/p>
什么時候使用界面?
- 安全性:?當我們必須簡單地隱藏某些功能并稍后使用它們時。有必要隱藏一些細節(jié),同時只顯示對用戶重要的細節(jié)。
- 多重繼承:在c#中,一個類可以繼承一個簡單的父類,繼承其所有功能。 C# 不支持多重繼承,原因很簡單,就是為了不讓 C# 變得復雜。但是通過使用接口,可以將多個接口實現到單個類中。
C# 接口通常包含以下元素:
- 聲明 – 在 C# 中,接口是定義一組方法簽名、屬性、事件或索引器的契約。它不包含任何實現,而是作為類要遵循的藍圖。實現接口的類必須為接口中聲明的所有成員提供具體的實現。
- 成員 – 接口成員是接口內聲明的方法、屬性、事件和索引器。他們定義了實現類必須遵守的契約,以確保不同類之間行為一致。實現類必須為這些成員提供具體的實現,促進代碼一致性,并實現多態(tài)性和代碼重用。
-
實現 – 實現接口的類必須為接口中聲明的所有成員提供實現。該類可以使用 :interfaceName 語法顯式指定它實現一個接口。例如:
public class MyClass : IMyInterface { public void Method1() { // Method implementation }public string Property1 { get; set; } public event EventHandler Event1; }
- 多重繼承:C# 通過接口支持多重繼承。一個類可以實現多個接口,從而允許它繼承多組方法簽名,而無需與實現的多重繼承相關的復雜性。這使得設計類具有更大的靈活性,同時避免了傳統(tǒng)多重繼承中固有的鉆石問題。
- 接口繼承:在 C# 中,接口繼承允許派生接口繼承一個或多個基接口中定義的方法簽名。實現派生接口的類必須提供所有繼承方法的實現。這使得能夠創(chuàng)建接口層次結構,促進代碼重用和對象設計的靈活性。
C# 接口示例
現在我們已經了解了什么是接口及其需求。讓我們演示一個帶有接口實現的 C# 代碼的簡單示例。
示例#1
程序實現接口并打印一條簡單的語句。
代碼:
using System; namespace MyApplication { interface SampleInterface { void InterfaceMethod(); } class Int_Example : SampleInterface { public void InterfaceMethod() { Console.WriteLine("\nThis is simple example of Interface in C#."); } } class Program { static void Main(string[] args) { Int_Example myInterface = new Int_Example(); myInterface.InterfaceMethod(); Console.Read(); } } }
代碼解釋:從使用和命名空間開始,生成一個基本接口作為 SampleInterface,其主體中有一個方法。接口內的該方法沒有任何特定的主體。然后我們有新的類來實現我們創(chuàng)建的接口。使用 class 關鍵字創(chuàng)建,后跟類名,然后使用冒號符號后跟接口名稱來實現接口。在我們的 Int_Example 類中,我們有之前創(chuàng)建的接口方法,當時它還沒有實體,現在我們添加了簡單的打印語句,它說:“這是 C# 中接口的一個簡單示例?!?/p>
Then begins our mail class, namely Program, with the static void main statement. Inside our main class, we have created a new object for our Int_Example class which inherits interface. The new object is created and to the next line, our method created earlier is called up. Finally, our newly created object will call the earlier created method and the body inside that method will be executed here. With Console.Read(); the program will wait for user input before exiting.
Output:
Upon successful compilation and execution, the program must simply print the statement: “This is a simple example of Interface in C#.”
Example #2
Arithmetic operations using the interface.
Code:
using System; namespace arth_interface { public interface SampleInterface { void sam_add(int a, int b); void sam_sub(int a, int b); void display(); } class interface_class : SampleInterface { int x, y; public void sam_add(int a, int b) { int m, n; m = a; n = b; x = m + n; } public void sam_sub(int a, int b) { int m, n; m = a; n = b; y = a - b; } public void display() { Console.WriteLine("Added Value is:" + x); Console.WriteLine("Subtracted value is:" + y); } } class arth_interface { static void Main(string[] args) { interface_class obj_interface_class = new interface_class(); int fnumber, snumber; Console.WriteLine("Please Enter 1st Number to perform Addition and Subtraction:"); fnumber = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Now 2nd Number to perform Addition and Subtraction:"); snumber = Convert.ToInt16(Console.ReadLine()); obj_interface_class.sam_add(fnumber, snumber); obj_interface_class.sam_sub(fnumber, snumber); obj_interface_class.display(); Console.ReadKey(); } } }
Code Interpretation: Similar to our first example, we have used and namespace statements, followed by the interface and its body with methods. We have two basic methods for addition and subtraction with void as return type, two integers inside every method, respectively. Next, we have our class which implements our interface.
We’ve declared two integers and then we have our first method to calculate addition. Here is the operation that needs to be done for addition and the same is for the subtraction. Then we have our display method, which consists of two print statements, printing addition and subtraction values of the numbers passed.
Finally, we have our class with the main method, where we initially created an object for our interface. Then the program prints “Please Enter the 1st Number to perform Addition and Subtraction:”, where the user inputs a first number and the later second number, for the purpose of calculations. With the object created earlier, the program calls the add and sub-methods from the interface and the same operations are done. At last, we have our display method, which displays our results as defined in the display method and ReadKey(); method holds up our program until any key is pressed.
Output:
Advantages
Below are some of the advantages given.
- One of the major advantages of Interface in C# is a better alternative to implement multiple inheritances.
- The interface enables the plug-and-play method.
- Complete Abstraction can be achieved by the implementation of Interface.
- Along with making our code easy to maintain, concept loose coupling can be achieved.
Conclusion
We have understood what Interface in C# is. The proper syntax for an interface along with an explanation. To wrap it up, Interfaces in C# are a way to fill the emptiness of multiple inheritances in the language. Later we learned why do we actually need the interface in C# followed by the examples to demonstrate the understanding of the interfaces. The first example was to demonstrate simple use of interface while with the second example we implemented arithmetic operations, followed by Code Interpretation and output screenshot.
以上是C# 接口的詳細內容。更多信息請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

多線程和異步的區(qū)別在于,多線程同時執(zhí)行多個線程,而異步在不阻塞當前線程的情況下執(zhí)行操作。多線程用于計算密集型任務,而異步用于用戶交互操作。多線程的優(yōu)勢是提高計算性能,異步的優(yōu)勢是不阻塞 UI 線程。選擇多線程還是異步取決于任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C#和C 的歷史與演變各有特色,未來前景也不同。1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注于性能和系統(tǒng)級編程。2.C#由微軟在2000年發(fā)布,結合C 和Java的優(yōu)點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注于開發(fā)者的生產力和云計算。

可以采用多種方法修改 XML 格式:使用文本編輯器(如 Notepad )進行手工編輯;使用在線或桌面 XML 格式化工具(如 XMLbeautifier)進行自動格式化;使用 XML 轉換工具(如 XSLT)定義轉換規(guī)則;或者使用編程語言(如 Python)進行解析和操作。修改時需謹慎,并備份原始文件。
