C#中接口(Interface)的深入詳解
定義
在 C# 語言中,類之間的繼承關(guān)系僅支持單重繼承,而接口是為了實(shí)現(xiàn)多重繼承關(guān)系設(shè)計(jì)的。一個(gè)類能同時(shí)實(shí)現(xiàn)多個(gè)接口,還能在實(shí)現(xiàn)接口的同時(shí)再繼承其他類,并且接口之間也可以繼承。無論是表示類之間的繼承還是類實(shí)現(xiàn)接口、接口之間的繼承,都使用“:”來表示。
接口定義了屬性、方法和事件,這些都是接口的成員。接口只包含了成員的聲明。成員的定義是派生類的責(zé)任。接口提供了派生類應(yīng)遵循的標(biāo)準(zhǔn)結(jié)構(gòu)。接口定義了語法合同 "是什么" 部分,派生類定義了語法合同 "怎么做" 部分。
定義接口語法:
interface 接口名稱
{
接口成員;
}
接口命名通常以 I 字母開頭,例如Itest。
接口成員,不允許使用 public、private、protected、internal 訪問修飾符,不允許使用 static、virtual、abstract、sealed 修飾符。不能定義字段,定義的方法不能包含方法體。
示例:定義一本書的接口,有id、名稱name、定價(jià)price幾個(gè)屬性,和一個(gè)方法售賣價(jià)SalePrice()。
using System; namespace app { interface IBook { int Id { get; set; } string Name { get; set; } double Price { get; set; } double SalePrice(int discount); } }
實(shí)現(xiàn)
接口的實(shí)現(xiàn)與類的繼承語法格式類似,也是重寫了接口中的方法,讓其有了具體的實(shí)現(xiàn)內(nèi)容。
實(shí)現(xiàn)接口語法:
class 類名 : 接口名
{
//類中的成員以及實(shí)現(xiàn)接口中的成員
}
在實(shí)現(xiàn)接口的成員時(shí)有兩種方式,隱式實(shí)現(xiàn)接口成員和顯式實(shí)現(xiàn)接口成員。隱式實(shí)現(xiàn)接口成員是將接口的所有成員以 public 訪問修飾符修飾。顯式實(shí)現(xiàn)接口是指在實(shí)現(xiàn)接口時(shí)所實(shí)現(xiàn)的成員名稱前含有接口名稱作為前綴。
下面對一本書的接口分別使用隱式實(shí)現(xiàn)和顯式實(shí)現(xiàn)。
隱式實(shí)現(xiàn):
class Book:IBook { // 隱式的實(shí)現(xiàn)接口中的屬性 public int Id { get; set; } public string Name { get; set; } public double Price { get; set; } // 隱式實(shí)現(xiàn)接口中的方法 public double SalePrice(int discount) { double salePrice = Price * discount * 0.1; return salePrice; } }
隱式實(shí)現(xiàn)后進(jìn)行運(yùn)行:
class Program { static void Main(string[] args) { Book book = new Book(); book.Id = 1001; book.Name = "tynam"; book.Price = 60; Console.WriteLine("id:{0}" , book.Id); Console.WriteLine("書名:{0}" , book.Name); Console.WriteLine("定價(jià):{0}", book.Price); Console.WriteLine("售賣價(jià):{0}", book.SalePrice(8)); } }
運(yùn)行后結(jié)果:
id:1001
書名:tynam
定價(jià):60
售賣價(jià):48
顯式實(shí)現(xiàn):
class Book:IBook { public double Price { get; set; } // 顯示的實(shí)現(xiàn)接口中的屬性 int IBook.Id { get; set; } string IBook.Name { get; set; } // 顯式實(shí)現(xiàn)接口中的方法 double IBook.SalePrice(int discount) { double salePrice = Price * discount * 0.1; return salePrice; } }
顯式實(shí)現(xiàn)后進(jìn)行運(yùn)行:
class Program { static void Main(string[] args) { Book book = new Book(); IBook bookDetail = book; bookDetail.Id = 1001; bookDetail.Name = "tynam"; bookDetail.Price = 60; Console.WriteLine("id:{0}" , bookDetail.Id); Console.WriteLine("書名:{0}" , bookDetail.Name); Console.WriteLine("定價(jià):{0}", bookDetail.Price); Console.WriteLine("售賣價(jià):{0}", bookDetail.SalePrice(8)); } }
運(yùn)行后結(jié)果:
id:1001
書名:tynam
定價(jià):60
售賣價(jià):48
注意:接口無法直接進(jìn)行實(shí)例化。 其成員由實(shí)現(xiàn)接口的任何類或結(jié)構(gòu)來實(shí)現(xiàn)。
多態(tài)
使用接口實(shí)現(xiàn)多態(tài) 需要滿足以下兩個(gè)條件。
定義接口并使用類實(shí)現(xiàn)了接口中的成員。
創(chuàng)建接口的實(shí)例指向不同的實(shí)現(xiàn)類對象。
示例:定義一個(gè)接口名稱為 IBird,分別定義兩個(gè)實(shí)現(xiàn)類 Phoenix 和 Duck 來實(shí)現(xiàn)接口的成員,代碼如下。
interface IBird { void fly(); } class Phoenix : IBird { public void fly() { Console.WriteLine("鳳凰會(huì)飛"); } } class Duck : IBird { public void fly() { Console.WriteLine("鴨子也會(huì)飛"); } }
實(shí)例化后執(zhí)行:
class Program { static void Main(string[] args) { IBird phoenix = new Phoenix(); phoenix.fly(); IBird duck = new Duck(); duck.fly(); } }
執(zhí)行結(jié)果:
鳳凰會(huì)飛
鴨子也會(huì)飛
總結(jié)
到此這篇關(guān)于C#中接口(Interface)深入詳解的文章就介紹到這了,更多相關(guān)C# 接口(Interface)詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例
下面小編就為大家?guī)硪黄狢#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01unity實(shí)現(xiàn)多點(diǎn)觸控代碼
這篇文章主要介紹了unity實(shí)現(xiàn)多點(diǎn)觸控代碼,我最近在學(xué)習(xí)Unity游戲引擎。先從Unity平面開始,本章介紹Unity 平面上的多點(diǎn)觸摸。有需要的小伙伴參考下。2015-03-03WPF+WriteableBitmap實(shí)現(xiàn)高性能曲線圖的繪制
這篇文章主要為大家詳細(xì)介紹了如何利用WPF+WriteableBitmap實(shí)現(xiàn)高性能曲線圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-08-08C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn)
這篇文章主要介紹了C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳解C#中的泛型以及編程中使用泛型的優(yōu)點(diǎn)
這篇文章主要介紹了詳解C#中的泛型以及編程中使用泛型的優(yōu)點(diǎn),對泛型的支持時(shí)C#語言中的重要特性,需要的朋友可以參考下2016-02-02