日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#顯式實(shí)現(xiàn)接口原理淺析

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

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、雁江ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的雁江網(wǎng)站制作公司

 
 
 
  1. void ITalk.Read()  

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

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

 
 
 
  1. theDoc.Read( );  

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

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

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

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

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

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

假設(shè)有如下兩個(gè)接口:

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

繼承 IDerived的類至少需要進(jìn)行一個(gè)顯示實(shí)現(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#顯式實(shí)現(xiàn)接口之實(shí)現(xiàn)接口的值類型(Struct)

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

 
 
 
  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#顯式實(shí)現(xiàn)接口之程序輸出:

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

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


新聞標(biāo)題:C#顯式實(shí)現(xiàn)接口原理淺析
標(biāo)題網(wǎng)址:http://www.5511xx.com/article/dpegees.html