自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#顯式實現(xiàn)接口原理淺析

開發(fā) 后端
C#顯式實現(xiàn)接口原理是什么呢?通過什么辦法顯式實現(xiàn)接口呢?那么本文就向你介紹具體的步驟和實現(xiàn)代碼。

C#顯式實現(xiàn)接口方法是什么情況呢?當一個類實現(xiàn)了兩個接口(假設Document 類實現(xiàn)了IStorable和ITalk接口),但是兩個接口中有方法名相同時,可以使用下面的語法來顯式地實現(xiàn)一個接口:

  1. void ITalk.Read()  

C#顯式實現(xiàn)接口的方法時,不可以加訪問修飾符(access modifier),將隱式地聲明為public。

不能通過類的實例來直接訪問顯式實現(xiàn)的方法。假設該類還實現(xiàn)了IStorable接口中的Read()的方法,當使用下面的語句時:

  1. theDoc.Read( );  

將會隱式調(diào)用IStorable的Read() 方法。

如果該類僅實現(xiàn)了ITalk接口,而沒有實現(xiàn)IStorable接口,也就不存在方法名沖突的情況,但是卻仍使用顯示的接口聲明,那么當使用 theDoc.Read() 時,將會出現(xiàn)編譯錯誤。

  1. 'ExplicitImplementation.Document  
  2.  
  3. ' does not contain a definition for   
  4.  
  5. 'Read' F:\MyApp\Test\ExplictImplament.cs  57  11  Test  

當想使用 ITalk接口的方法時,需要進行一次類型轉(zhuǎn)換,使用下面的語法:

  1. ITalk itDoc = theDoc;   
  2. itDoc.Read();  

C#顯式實現(xiàn)接口之成員隱藏

假設有如下兩個接口:

  1. interface IBase   
  2.  
  3. {   
  4.    int P { getset; }   
  5. }   
  6.  
  7. interface IDerived : IBase   
  8.  
  9. {  
  10.  
  11.    new int P();   
  12.  
  13. }   

繼承 IDerived的類至少需要進行一個顯示實現(xiàn)。

  1. class myClass : IDerived   
  2.  
  3. it55.com  
  4.  
  5. {   
  6.  
  7.    int IBase.P { get {...} }   
  8.  
  9.    public int P( ) {...}  
  10.  
  11. }   
  12.  
  13. class myClass : IDerived   
  14.  
  15.  
  16. {   
  17.  
  18.    public int P { get {...} }   
  19.  
  20.    int IDerived.P( ) {...}   
  21.  
  22. }  
  23.  
  24. class myClass : IDerived   
  25.  
  26. {  
  27.  
  28.    int IBase.P { get {...} }   
  29.  
  30.    int IDerived.P( ) {...}   
  31.  
  32. }  

C#顯式實現(xiàn)接口之實現(xiàn)接口的值類型(Struct)

如果使用值類型實現(xiàn)接口,則應通過值類型的對象訪問接口方法,而不要轉(zhuǎn)換成接口,再用接口進行訪問,此時會多出一個“復制”了的引用對象,而原來的值對象依然存在,兩個對象是各自獨立的。

  1. myStruct theStruct = new myStruct( );   
  2.  
  3. theStruct.Status = 2;   
  4.  
  5. IStorable isTemp = ( IStorable ) theStruct;   
  6.  
  7. it55.com  
  8.  
  9. Console.WriteLine( "isTemp: {0}", isTemp.Status );   
  10.  
  11. isTemp.Status = 4;   
  12.  
  13. Console.WriteLine("theStruct:{0},   
  14.  
  15. isTemp: {1}",theStruct.Status, isTemp.Status );   
  16.  
  17. theStruct.Status = 6;   
  18.  
  19. Console.WriteLine( "theStruct: {0},   
  20.  
  21. isTemp: {1}",theStruct.Status, isTemp.Status );   

C#顯式實現(xiàn)接口之程序輸出:

  1. isTemp: 2   
  2. theStruct: 2, isTemp: 4   
  3. theStruct: 6, isTemp: 4  

C#顯式實現(xiàn)接口的相關(guān)內(nèi)容就向你介紹到這里,希望對你了解和學習C#顯式實現(xiàn)接口有所幫助。

【編輯推薦】

  1. 詳解C#break ,continue, return
  2. 淺析C#接口特點及實例應用
  3. C# interface實例淺析
  4. C# interface編程應用淺析
  5. C#interface定義及使用淺析
責任編輯:仲衡 來源: it55.com
相關(guān)推薦

2009-08-24 10:06:31

C#接口成員

2009-09-01 18:29:24

C#實現(xiàn)多個接口

2009-08-31 16:48:02

C#實現(xiàn)IDispos

2009-08-27 18:09:49

C#接口的實現(xiàn)

2009-09-07 05:24:22

C#窗體繼承

2009-08-07 08:53:52

C# ICloneab

2009-08-27 17:59:56

C#接口定義

2009-08-31 17:02:28

C#接口編程

2009-08-24 15:12:13

C# 泛型接口

2009-09-28 14:45:22

C#接口的定義

2009-09-03 17:40:25

C#發(fā)送短信

2009-09-02 15:34:37

C#實現(xiàn)插件構(gòu)架

2009-09-04 10:05:16

C#調(diào)用瀏覽器瀏覽器的原理

2009-09-18 19:21:17

C#接口

2010-01-14 17:13:53

C++接口

2009-08-27 13:05:06

C#接口特點C#接口實例

2009-08-31 16:37:20

C#接口定義

2011-06-03 10:15:13

2009-08-31 15:55:17

C#實現(xiàn)Strateg

2009-08-25 17:55:52

C#實現(xiàn)Strateg
點贊
收藏

51CTO技術(shù)棧公眾號