国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
為什麼我們需要 C# 介面?
C# 介面通常包含以下元素:
C# 介面範例
範例#1
Example #2
Advantages
Conclusion

C# 介面

Sep 03, 2024 pm 03:30 PM
c# c# tutorial

Interface,在C#中是一個關鍵字,它包含一組抽象方法和屬性,這些方法和屬性由抽像或非抽象類別實作或使用。定義方法是介面內的屬性,預設情況下它們是公共和抽象的。

用最簡單的話來說,介面就像一個契約,主體中包含的每個成員或元件都必須遵循契約,它定義了必須做什麼。該介面不包含任何字段,並且始終透過使用關鍵字“interface”來定義。

文法:

語法以介面關鍵字開頭,後面跟著介面名稱,然後是正文。

interface <name_for_interface>
{
//abstract methods
//abstract properties.
}

如您所見,我們有 C# 中介面的標準語法,它以「interface」關鍵字開頭,然後是介面的名稱,然後是主體內的抽象方法和屬性。在 C# 中,可以在類別或結構內部實作和使用多個介面。這些介面可以將各種方法、索引器、屬性以及事件作為成員。

為什麼我們需要 C# 介面?

基本上我們已經明白介面內部沒有特定的功能,如果是這樣,那我們?yōu)槭颤N需要介面?

什麼時候使用介面?

  • 安全性:?當我們必須簡單地隱藏某些功能並稍後使用它們時。有必要隱藏一些細節(jié),同時只顯示對使用者重要的細節(jié)。
  • 多重繼承:在c#中,一個類別可以繼承一個簡單的父類,繼承其所有功能。 C# 不支援多重繼承,原因很簡單,就是為了不讓 C# 變得複雜。但是透過使用接口,可以將多個接口實現到單一類別。

C# 介面通常包含以下元素:

  1. 宣告 – 在 C# 中,介面是定義一組方法簽章、屬性、事件或索引器的契約。它不包含任何實現,而是作為類別要遵循的藍圖。實作介面的類別必須為介面中聲明的所有成員提供具體的實作。
  2. 成員 – 介面成員是介面內宣告的方法、屬性、事件和索引器。他們定義了實現類別必須遵守的契約,以確保不同類別之間行為一致。實現類別必須為這些成員提供具體的實現,促進程式碼一致性,並實現多態(tài)性和程式碼重複使用。
  3. 實作 – 實作介面的類別必須為介面中宣告的所有成員提供實作。該類別可以使用 :interfaceName 語法明確指定它實作一個介面。例如:
    public class MyClass : IMyInterface
    {
    public void Method1()
    {
    // Method implementation
    }public string Property1 { get; set; }
    
    public event EventHandler Event1;
    }
  4. 多重繼承:C# 透過介面支援多重繼承。一個類別可以實現多個接口,從而允許它繼承多組方法簽名,而無需與實現的多重繼承相關的複雜性。這使得設計類別具有更大的靈活性,同時避免了傳統(tǒng)多重繼承中固有的鑽石問題。
  5. 介面繼承:在 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 類別中,我們有先前建立的介面方法,當時它還沒有實體,現在我們加入了簡單的列印語句,它說:「這是 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:

C# 介面

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:

C# 介面

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數產生器指南。在這裡,我們討論隨機數產生器的工作原理、偽隨機數和安全數的概念。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

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

C# 中的質數 C# 中的質數 Sep 03, 2024 pm 03:35 PM

C# 質數指南。這裡我們討論c#中素數的介紹和範例以及程式碼實作。

C#與C:歷史,進化和未來前景 C#與C:歷史,進化和未來前景 Apr 19, 2025 am 12:07 AM

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ā)者的生產力和雲計算。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

xml怎麼改格式 xml怎麼改格式 Apr 03, 2025 am 08:42 AM

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

C# 中的回文 C# 中的回文 Sep 03, 2024 pm 03:34 PM

C# 回文指南。在這裡,我們討論 C# 中回文背後的介紹和邏輯,以及各種方法及其程式碼。

See all articles