简单的实例理解接口的伟大意义

来源:互联网 发布:远程视频监控软件 编辑:程序博客网 时间:2024/05/16 23:35

首先,我们必须明确,接口是一个类。

 

“接口是一个特殊的类,又是一个特别有意义的类,不是因为它的特殊,而是因为它的意义,叫它接口更合适,但不能忘了,它仍是类。”

 

“接口是一个只有声明,没有实现的类。”

 

很多人纠结于接口只是一个标准,是一个契约,而忘记了它的意义。

 

下面我们来看这样一个问题:

话说有家影视公司选拔偶像派男主角,导演说了,男演员,身高是王道。于是有下面代码:

[csharp] view plaincopyprint?
  1. public class Actor  
  2. {  
  3.     private string name;  
  4.     private int height;  
  5.   
  6.     public Actor(string name, int height)  
  7.     {  
  8.         this.name = name;  
  9.         this.height = height;  
  10.     }  
  11.     public string Name  
  12.     {  
  13.         get { return this.name; }  
  14.     }  
  15.     public int Height  
  16.     {  
  17.         get { return this.height; }  
  18.     }  
  19.       
  20.     public int CompareTo(object obj)  
  21.     {  
  22.         return this.height - ((Actor)obj).height;  
  23.     }  
  24.   
  25.     public string GetName()  
  26.     {  
  27.         return this.name;  
  28.     }  
  29. }  
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行).

 

 

现在的问题是,明天又要选拨女演员了,导演说了,女演员,苗条是王道。女演员的这个类,你肯定是要做的,只是….

 

只是,我刚才略过去的,让你编写的代码,你是不是还要再重新编写呢????

 

这等于又重新编写了一个程序。

 

这时,我们就想到了接口,我们来接着看代码吧:

我先做一个接口,这个接口:

 

[csharp] view plaincopyprint?
  1. namespace WestGarden.IPlayer  
  2. {  
  3.     public interface ISelectPlayer  
  4.     {  
  5.         string GetName();  
  6.   
  7.         int CompareTo(object obj);  
  8.     }  
  9. }  
namespace WestGarden.IPlayer{    public interface ISelectPlayer    {        string GetName();        int CompareTo(object obj);    }}


这个接口,定义了两个函数,一个,当然是要进行比较,标准由你定,你说是导演定的,那更好,不用你费脑子了。

 

我们把刚才做的男演员的类,按照这个接口的标准来实现,也就是继承这个接口:

[csharp] view plaincopyprint?
  1. using System;  
  2.   
  3. using WestGarden.IPlayer;  
  4.   
  5. namespace WestGarden.DAL  
  6. {  
  7.     public class Actor:ISelectPlayer  
  8.     {  
  9.         private string name;  
  10.         private int height;  
  11.   
  12.         public Actor(string name, int height)  
  13.         {  
  14.             this.name = name;  
  15.             this.height = height;  
  16.         }  
  17.         public string Name  
  18.         {  
  19.             get { return this.name; }  
  20.         }  
  21.         public int Height  
  22.         {  
  23.             get { return this.height; }  
  24.         }  
  25.          
  26.         public int CompareTo(object obj)  
  27.         {  
  28.             return this.height - ((Actor)obj).height;  
  29.         }  
  30.   
  31.         public string GetName()  
  32.         {  
  33.             return this.name;  
  34.         }  
  35.     }  
  36. }  
using System;using WestGarden.IPlayer;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;        }    }}


顺手,把女演员的类也做了吧:

[csharp] view plaincopyprint?
  1. using System;  
  2.   
  3. using WestGarden.IPlayer;  
  4.   
  5. namespace WestGarden.DAL  
  6. {  
  7.     public class Actress:ISelectPlayer  
  8.     {  
  9.         private string name;  
  10.         private int weight;  
  11.           
  12.         public Actress(string name, int weight){  
  13.             this.name = name;  
  14.             this.weight = weight;  
  15.         }  
  16.   
  17.         public string Name  
  18.         {  
  19.             get { return this.name; }  
  20.         }  
  21.         public int Weight  
  22.         {  
  23.             get { return this.weight; }  
  24.         }  
  25.   
  26.     
  27.         public int CompareTo(object obj)  
  28.         {  
  29.             return ((Actress)obj).weight - this.weight;  
  30.         }  
  31.   
  32.         public string GetName()  
  33.         {  
  34.             return this.name;  
  35.         }  
  36.     }  
  37. }  
using System;using WestGarden.IPlayer;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;        }    }}


这时,我们在应用层这样编写代码:

[csharp] view plaincopyprint?
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     Actor actor1 = new Actor("潘长江", 150);  
  4.     Actor actor2 = new Actor("刘德华", 180);  
  5.   
  6.     Actress actress1 = new Actress("巩俐", 120);  
  7.     Actress actress2 = new Actress("周迅", 80);  
  8.   
  9.     WhoIsBetter(actor1, actor2);  
  10.   
  11.     WhoIsBetter(actress1, actress2);  
  12. }  
  13.   
  14. public void WhoIsBetter(ISelectPlayer a, ISelectPlayer b)  
  15. {  
  16.     if (a.CompareTo(b) > 0)  
  17.         Response.Write(a.GetName());  
  18.     else  
  19.         Response.Write(b.GetName());  
  20. }  
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);    WhoIsBetter(actor1, actor2);    WhoIsBetter(actress1, actress2);}public void WhoIsBetter(ISelectPlayer a, ISelectPlayer b){    if (a.CompareTo(b) > 0)        Response.Write(a.GetName());    else        Response.Write(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

在些一并感谢!

版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.

原创粉丝点击