C# interface學(xué)習(xí)經(jīng)驗(yàn)淺談
C# interface是把所需成員組合起來(lái),以封裝一定功能的集合。它好比一個(gè)模板,在其中定義了對(duì)象必須實(shí)現(xiàn)的成員,通過(guò)類或結(jié)構(gòu)來(lái)實(shí)現(xiàn)它。接口不能直接實(shí)例化,即ICount ic=new iCount()是錯(cuò)的。接口不能包含成員的任何代碼,只定義成員本身。接口成員的具體代碼由實(shí)現(xiàn)接口的類提供。接口使用interface關(guān)鍵字進(jìn)行聲明。聲明格式如下:
- [attributes] [modifiers]
- interface identifier
- [: base-list] {interface-body} {;}
C# interface成員的默認(rèn)訪問(wèn)方式是public,在聲明接口成員時(shí)不能出現(xiàn)abstract、public、protected、internal、private、virtual、override或static等關(guān)鍵字。接口成員可以是方法、屬性、索引指示器或事件,不能是字段,而且接口的成員名不能相同。
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Interface
- {
- interface ICount
- {
- void Count();//接口成員的默認(rèn)訪問(wèn)方式是public
- //int number;//接口中不能定義字段成員
- int para { get;set;}
- }
- class Double : ICount
- {
- public void Count()
- { //實(shí)現(xiàn)ICount的Count()方法
- Console.WriteLine("The double para is {0}",2*para);
- }
- int p;
- public int para
- {
- get { return p; }
- set { p = value; }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Double d = new Double();
- d.para = 10;//給"屬性"賦值
- d.Count();
- ICount ic = (ICount)d;//轉(zhuǎn)換為接口
- ic.para = 5;
- ic.Count();
- Console.ReadLine();
- }
- }
- }
C# interface的一點(diǎn)使用總結(jié)
1 一個(gè)類可以實(shí)現(xiàn)一個(gè)以上的接口;
2 類必須實(shí)現(xiàn)接口中的“所有”屬性和方法;
3 屬性和方法定義所采用的格式必須與接口定義所采用的格式完全相同。方法所采用的參數(shù)數(shù)目及參數(shù)類型必須與接口中的完全相同。方法的名稱也必須相同。
接口之間的繼承:接口的繼承僅僅說(shuō)明了接口之間的繼承關(guān)系,派生的接口繼承了父接口的成員說(shuō)明,沒(méi)有繼承父接口的實(shí)現(xiàn)。private和internal類型的接口不允許繼承。如果派生接口中準(zhǔn)備重寫(xiě)父接口的方法,實(shí)現(xiàn)方式同類的繼承成員的覆蓋。
如果一個(gè)類實(shí)現(xiàn)了某個(gè)接口,即使父接口沒(méi)有在類的基類表中列出,這個(gè)類也隱式地繼承了接口的所有父接口。
如果兩個(gè)接口A和B含有同名的成員Method,且都由同一個(gè)類C實(shí)現(xiàn),則類C必須分別為A和B的Method成員提供單獨(dú)的實(shí)現(xiàn),即顯式實(shí)現(xiàn)接口成員??尚蟹桨福?/P>
(1)直接實(shí)現(xiàn)一個(gè)接口的成員,顯式實(shí)現(xiàn)另一個(gè)接口的成員;
(2)顯式實(shí)現(xiàn)兩個(gè)接口的成員
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Interface
- {
- interface IEnglish
- {
- float Length();
- float Width();
- }
- interface IMetric
- {
- float Length();
- float Width();
- }
- class Class1 : IEnglish, IMetric
- {
- float lengthInches;
- float widthInches;
- public Class1(float length, float width)
- {
- lengthInches = length;
- widthInches = width;
- }
- //顯式實(shí)現(xiàn)IEnglish的成員
- float IEnglish.Length()
- {
- return lengthInches;
- }
- float IEnglish.Width()
- {
- return widthInches;
- }
- //顯式實(shí)現(xiàn)IMetric的成員
- float IMetric.Length()
- {
- return lengthInches * 2.54f;
- }
- float IMetric.Width()
- {
- return widthInches * 2.54f;
- }
- static void Main(string[] args)
- {
- Class1 c1 = new Class1(30.0f,20.0f);
- IEnglish e=(IEnglish)c1;
- IMetric m=(IMetric )c1;
- Console.WriteLine("Length(in):{0}",e.Length());
- Console.WriteLine("Width(in):{0}",e.Width());
- Console.WriteLine("Length(cm):{0}",m.Length());
- Console.WriteLine("Width(cm):{0}",m.Width());
- Console.ReadLine();
- }
- }
- }
執(zhí)行結(jié)果:
C# interface學(xué)習(xí)的一些體會(huì)和具體的實(shí)例演示就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# interface有所幫助。
【編輯推薦】