.NET中的三種介面實現方式

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   io   ar   

摘自:http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html 

一般來說.NET提供了三種不同的介面實現方式,分別為隱式介面實現、明確介面實作、混合式介面實現。這三種方式各有各的特點。

首先來看隱式介面實現,這恐怕是我們使用最多的一種介面實現,因為隱匿介面實現是.NET的預設介面實現方式。下面讓我們來看一個隱式介面實現的例子:

using System;internal class MyClass{public void SomeMethod(){// 利用介面的方式聲明一個Myinplement對象IMyInterface iObj = new MyInplement();iObj.MethodA();// 利用類的方式聲明一個Myinplement對象MyInplement obj = new MyInplement();obj.MethodB();}}public class MyInplement : IMyInterface{#region IMyInterface Members/// <summary>/// 利用隱式介面實現方式實現介面中的方法/// </summary>public void MethodA(){Console.WriteLine("a");}/// <summary>/// 利用隱式介面實現方式實現介面中的方法/// </summary>public void MethodB(){Console.WriteLine("B");}#endregion}public interface IMyInterface{void MethodA();void MethodB();}

 

上面的代碼很簡單,我們聲明了一個介面,然後利用MyImplement類來實現介面。在實現介面的時候我們選擇了隱式介面實現,即在介面中定義的方法簽名前加上修飾符來定義實現方法的簽名,比如public voidMethodB()。這種隱式介面實現用起來很簡單,但卻有一個缺點,就是它允許客戶可以直接調用實作類別中的方法,(即通過MyInplementobj = newMyInplement()這樣的方式聲明對象)而不是通過介面調用實作類別中的方法(即通過的IMyInterface iObj = new MyInplement();方式來聲明對象),這無疑增加了代碼的耦合性和違反了面向介面編程的原則。那怎麼樣才能禁止客戶直接調用實作類別呢? 下面我們看的明確介面實作就可以。

明確介面實作是指實作類別在實現介面的方法時在方法的簽名上用"介面名.方法名”的樣式來定義方法名,同時在方法的簽名中不允許帶有修飾符,因為所有明確介面實作的方法全是private的,這是.NET為了禁止客戶直接調用實作類別而硬性規定的。下面我們來看明確介面實作的例子:

using System;internal class MyClass{public void SomeMethod(){// 利用介面的方式聲明一個Myinplement對象IMyInterface iObj = new MyInplement();iObj.MethodA();// 利用類的方式聲明一個Myinplement對象,會報錯,因為MyImplement的MethodA方法是private的MyInplement obj = new MyInplement();obj.MethodB();}}public class MyInplement : IMyInterface{#region IMyInterface Members/// <summary>/// 利用明確介面實作方式實現介面中的方法/// </summary>void IMyInterface.MethodA(){Console.WriteLine("a");}/// <summary>/// 利用明確介面實作方式實現介面中的方法/// 會報錯,因為在明確介面實作時不允許帶有修飾符/// </summary>public  void IMyInterface.MethodB(){Console.WriteLine("B");}#endregion}public interface IMyInterface{void MethodA();void MethodB();}

 

上面的代碼中有兩個地方會報錯,這是因為我們在報錯的地方違反了明確介面實作的約束。

最後讓我們來看混合式介面實現,混合式介面實現就是將隱式介面實現和明確介面實作搭在一起使用,下面讓我們來看一個例子:

using System;internal class MyClass{public void SomeMethod(){// 利用介面的方式聲明一個Myinplement對象IMyInterface iObj = new MyInplement();iObj.MethodA();// 利用類的方式聲明一個Myinplement對象,不會報錯,因為MyImplement的MethodA方法是public的MyInplement obj = new MyInplement();obj.MethodB();}}public class MyInplement : IMyInterface{#region IMyInterface Members/// <summary>/// 利用明確介面實作方式實現介面中的方法/// </summary>void IMyInterface.MethodA(){Console.WriteLine("a");}/// <summary>/// 利用隱式介面實現方式實現介面中的方法/// </summary>public  void MethodB(){Console.WriteLine("B");}#endregion}public interface IMyInterface{void MethodA();void MethodB();}

 

上面的代碼中我們將MehtodB的實現改為隱式介面實現,這樣MyImplement.Method的修飾符就是public的了,代碼中就沒有錯了。

通過簡單的程式碼範例看到了三種介面實現方式的區別,話說在今天之前我一直沒有注意到隱式介面實現可以允許實作類別直接被調用,而明確介面實作由於實作類別中的方法是私人的所以不能被直接調用。看來我對.NET的認識還很初級啊,有許多基礎性的知識還沒有掌握啊。以後要努力了。

 

 

 

 

 ----------->>>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.