知识大全 简单的实例理解接口的伟大意义
Posted 意义
篇首语:身不饥寒,天未曾负我;学无所获,我何以对天。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 简单的实例理解接口的伟大意义相关的知识,希望对你有一定的参考价值。
首先 我们必须明确 接口是一个类 接口是一个特殊的类 又是一个特别有意义的类 不是因为它的特殊 而是因为它的意义 叫它接口更合适 但不能忘了 它仍是类 接口是一个只有声明 没有实现的类 很多人纠结于接口只是一个标准 是一个契约 而忘记了它的意义 下面我们来看这样一个问题 话说有家影视公司选拔偶像派男主角 导演说了 男演员 身高是王道 于是有下面代码 [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 actor = new Actor( 潘长江 ) Actor actor = new Actor( 刘德华 ) Actress actress = new Actress( 巩俐 ) Actress actress = new Actress( 周迅 ) WhoIsBetter(actor actor ) WhoIsBetter(actress actress ) public void WhoIsBetter(ISelectPlayer a ISelectPlayer b) if (a CompareTo(b) > ) Response Write(a GetName()) else Response Write(b GetName()) protected void Page_Load(object sender EventArgs e) Actor actor = new Actor( 潘长江 ) Actor actor = new Actor( 刘德华 ) Actress actress = new Actress( 巩俐 ) Actress actress = new Actress( 周迅 ) WhoIsBetter(actor actor ) WhoIsBetter(actress actress ) public void WhoIsBetter(ISelectPlayer a ISelectPlayer b) if (a CompareTo(b) > ) Response Write(a GetName()) else Response Write(b GetName()) 注意 我们做的这个函数 publicvoid WhoIsBetter(ISelectPlayer a ISelectPlayer b) 这个函数 形参是ISelectPlayer 是接口 我认为 接口的意义 就在这里 你实现接口的类是男演员也好 女演员也好 男主角也好 女主角也好 男配角也好 女本角也好 男群众演员也好 女群众演员也好 只要你继承的是我这个ISelectPlayer 或者 你习惯于说 遵守了我这个接口的标准 或者契约 我这段代码 都不需要改变!! 这和那个比方是一样的 不管你插在USB接口的是U盘 还是移动硬盘 还是什么mp 还是mp 还是你新发明的什么东西 只要你能插在我的USB口上 我主机都不需要做任何改变 直接在上面读取或者写入数据 这个 是硬件接口的意义所在 也是我们这个ISelectPlayer类的意义所在 因为它有了这个伟大的意义 才把它改叫为接口的 因为 它象USB接口一样工作著…… cha138/Article/program/net/201311/13799相关参考