ASP.NET基础知识:简单的实例理解接口的伟大意义
首先,我们必须明确,接口是一个类。
“接口是一个特殊的类,又是一个特别有意义的类,不是因为它的特殊,而是因为它的意义,叫它接口更合适,但不能忘了,它仍是类。”
“接口是一个只有声明,没有实现的类。”
很多人纠结于接口只是一个标准,是一个契约,而忘记了它的意义。
下面我们来看这样一个问题:
话说有家影视公司选拔偶像派男主角,导演说了,男演员,身高是王道。于是有下面代码:
[csharp] 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;
}
}
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),因为,我们要比较男演员的身高,用身高判断哪个演员更好。
有了这个类,后面,你可以比较轻松地编写代码,判断是刘德华更优秀,还是潘长江更优秀了,这个代码,我这里就略过去了….
现在的问题是,明天又要选拨女演员了,导演说了,女演员,苗条是王道。女演员的这个类,你肯定是要做的,只是….
只是,我刚才略过去的,让你编写的代码,你是不是还要再重新编写呢????
这等于又重新编写了一个程序。
这时,我们就想到了接口,我们来接着看代码吧:
我先做一个接口,这个接口:
[csharp] namespace westgarden.iplayer
{
public interface iselectplayer
{
string getname();
int compareto(object obj);
}
}
namespace westgarden.iplayer
{
public interface iselectplayer
{
string getname();
int compareto(object obj);
}
}
这个接口,定义了两个函数,一个,当然是要进行比较,标准由你定,你说是导演定的,那更好,不用你费脑子了。
我们把刚才做的男演员的类,按照这个接口的标准来实现,也就是继承这个接口:
[csharp] 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;
}
}
}
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] 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;
}
}
}
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] 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());
}
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/
文中实例创意来源于:
在些一并感谢!
作者 yousuosi
上一篇: 作业三大定律
下一篇: 你将来想找一份什么样的工作?