ASP.NET基礎知識:簡單的實例理解接口的偉大意義
WestGarden 2012-5-26
http://www.cnblogs.com/WestGarden/
源代碼:13033480群共享
首先,我們必須明確,接口是一個類。
“接口是一個特殊的類,又是一個特別有意義的類,不是因為它的特殊,而是因為它的意義,叫它接口更合適,但不能忘了,它仍是類。”
“接口是一個只有聲明,沒有實現的類。”
很多人糾結於接口只是一個標准,是一個契約,而忘記了它的意義。
下面我們來看這樣一個問題:
話說有家影視公司選拔偶像派男主角,導演說了,男演員,身高是王道。於是有下面代碼:
public class Actor { private string name; private int height; public Actor(string name, int height) { this.name = name; this.height = height; } public string Name { get { return this.name; } } public int Height { get { return this.height; } } public int CompareTo(object obj) { return this.height - ((Actor)obj).height; } public string GetName() { return this.name; } }
這個類,除了可以存放男演員的基本信息,還定義了一個函數publicint CompareTo(object obj),因為,我們要比較男演員的身高,用身高判斷哪個演員更好。
有了這個類,后面,你可以比較輕松地編寫代碼,判斷是劉德華更優秀,還是潘長江更優秀了,這個代碼,我這里就略過去了….
(兒童不宜,此處省略1000行)……………….
現在的問題是,明天又要選撥女演員了,導演說了,女演員,苗條是王道。女演員的這個類,你肯定是要做的,只是….
只是,我剛才略過去的,讓你編寫的代碼,你是不是還要再重新編寫呢????
這等於又重新編寫了一個程序。
這時,我們就想到了接口,我們來接着看代碼吧:
我先做一個接口:
using System; namespace WestGarden.IDAL { public interface ISelectPlayer { string GetName(); int CompareTo(object obj); } }
這個接口,定義了兩個函數,一個,當然是要進行比較,標准由你定,你說是導演定的,那更好,不用你費腦子了。
我們把剛才做的男演員的類,按照這個接口的標准來實現,也就是繼承這個接口:
using System; using WestGarden.IDAL; namespace WestGarden.DAL { public class Actor:ISelectPlayer { private string name; private int height; public Actor(string name, int height) { this.name = name; this.height = height; } public string Name { get { return this.name; } } public int Height { get { return this.height; } } public int CompareTo(object obj) { return this.height - ((Actor)obj).height; } public string GetName() { return this.name; } } }
順手,把女演員的類也做了吧:
using System; using WestGarden.IDAL; namespace WestGarden.DAL { public class Actress:ISelectPlayer { private string name; private int weight; public Actress(string name, int weight){ this.name = name; this.weight = weight; } public string Name { get { return this.name; } } public int Weight { get { return this.weight; } } public int CompareTo(object obj) { return ((Actress)obj).weight - this.weight; } public string GetName() { return this.name; } } }
這時,我們在應用層這樣編寫代碼:
using System; using WestGarden.IDAL; using WestGarden.DAL; namespace WestGarden.Web { public partial class Select : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Actor actor1 = new Actor("潘長江", 150); Actor actor2 = new Actor("劉德華", 180); Actress actress1 = new Actress("鞏俐", 120); Actress actress2 = new Actress("周迅", 80); Response.Write("最佳男演員是:"+WhoIsBetter(actor1, actor2)+"</br>"); Response.Write("最佳女演員是:"+WhoIsBetter(actress1, actress2)+"</br>"); } //這里就象一個USB口一樣工作着,無論你插上的是男演員、女演員...,只要它繼承的是ISelectPlayer接口。 public string WhoIsBetter(ISelectPlayer a, ISelectPlayer b) { if (a.CompareTo(b) > 0) return a.GetName(); else return b.GetName(); } } }
注意:
我們做的這個函數,publicvoid WhoIsBetter(ISelectPlayer a,ISelectPlayer b)
這個函數,形參是ISelectPlayer,是接口,我認為,接口的意義,就在這里。
你實現接口的類是男演員也好,女演員也好,男主角也好、女主角也好、男配角也好、女配角也好、男群眾演員也好、女群眾演員也好,只要你繼承的是我這個ISelectPlayer,或者,你習慣於說,遵守了我這個接口的標准、或者契約,我這段代碼,都不需要改變!!
這和那個比方是一樣的,不管你插在USB接口的是U盤,還是移動硬盤,還是什么mp3,還是mp4,還是你新發明的什么東西,只要你能插在我的USB口上,我主機都不需要做任何改變,直接在上面讀取或者寫入數據。
這個,是硬件接口的意義所在,也是我們這個ISelectPlayer類的意義所在,因為它有了這個偉大的意義,才把它改叫為接口的,因為,它象USB接口一樣工作着……
文中觀點思想來源於博客:
http://www.cnblogs.com/WestGarden/
文中實例創意來源於:
http://www.cnblogs.com/wu-jian/archive/2012/05/24/2516284.html
在此一並表示感謝!