接口的好处与实例操作
来源:互联网 发布:js获取div的class 编辑:程序博客网 时间:2024/06/08 14:27
接口的有点:
@1、规范继承制必须实现共有的方法、多继承可实现继承者实现不同功能
@2、管理者只需规范接口,把实现交给程序员即可完成整体统一
例子下载:
http://download.csdn.net/detail/ggbb190/6549981
1、制作接口,规范动物通用行为,用来规范所有动物的共性,要求继承者统一实现
interface ianimalbehavior { /// <summary> /// 吃 /// </summary> string eat(); string drink(); string play(); string sleep(); }
2、定义狗dog的类、猫cat的类,集成子动物行为接口
class dog : ianimalbehavior { public string name = ""; public string eat() { return name+"已eat"; } public string drink() { return name + "已drink"; } public string play() { return name + "已play"; } public string sleep() { return name + "已sleep"; } public string jump() { return name + "已jump"; } }
class cat:ianimalbehavior { public string name = ""; public string eat() { return name+"已eat"; } public string drink() { return name + "已drink"; } public string play() { return name + "已play"; } public string sleep() { return name + "已sleep"; } }3、实际使用是这样的
cat littlecat = new cat(); littlecat.name = "猫"; this.textBox1.Text += littlecat.drink()+"\r\n"; this.textBox1.Text += littlecat.eat() + "\r\n"; this.textBox1.Text += littlecat.play() + "\r\n"; this.textBox1.Text += littlecat.sleep() + "\r\n"; dog littledog = new dog(); littledog.name = "littledog"; this.textBox1.Text += littledog.drink() + "\r\n"; this.textBox1.Text += littledog.eat() + "\r\n"; this.textBox1.Text += littledog.play() + "\r\n"; this.textBox1.Text += littledog.sleep() + "\r\n";
4、突然过几天又发现猫要抓老鼠,但是代码由于不是同一批程序员写的,所有不想更改以前的基类代码,所有抓老鼠的功能我们既可以写到cat中的方法,也可以抽象出一个抓老鼠接口,因为这样将来有蛇、老鼠夹、耗子药的对象出现都可以继承抓老鼠接口。为什么不写到ianimalbehavior接口呢?因为如果写在这里dog或其他动物必须实现该方法,但其本身并不具有该功能,所以不能抽象到这个接口内,而是新建一个接口,让cat多继承自不同接口。
class cat:ianimalbehavior,ianimalcatch { public string name = ""; public string eat() { return name+"已eat"; } public string drink() { return name + "已drink"; } public string play() { return name + "已play"; } public string sleep() { return name + "已sleep"; } public string catchmouse() { return name + "已catchmouse"; } }
5、最终实现
cat littlecat = new cat(); littlecat.name = "猫"; this.textBox1.Text += littlecat.drink()+"\r\n"; this.textBox1.Text += littlecat.eat() + "\r\n"; this.textBox1.Text += littlecat.play() + "\r\n"; this.textBox1.Text += littlecat.sleep() + "\r\n"; //新增 this.textBox1.Text += littlecat.catchmouse() + "\r\n"; dog littledog = new dog(); littledog.name = "littledog"; this.textBox1.Text += littledog.drink() + "\r\n"; this.textBox1.Text += littledog.eat() + "\r\n"; this.textBox1.Text += littledog.play() + "\r\n"; this.textBox1.Text += littledog.sleep() + "\r\n";
- 接口的好处与实例操作
- 实例讲解asp.net 接口的作用(好处)
- 接口的好处
- 接口的好处之一
- 接口编程的好处
- 接口编程的好处
- 接口的好处
- 接口编程的好处
- 接口的好处
- 面向接口的好处
- 接口编程的好处
- 使用接口的好处
- 使用接口的好处
- 使用接口的好处
- Java 接口(interface)的作用与好处
- 基本概念的澄清 1。抽象类与接口在设计时各有什么好处? 2。静态方法与实例方法的区别(论坛帖抽取)
- 原子操作的好处
- 接口的好处(转)
- 1114
- 内核内存检测
- 一股热流流变了我的全身
- SQLLite教程基础篇
- 本该到客运中心的却到了西站
- 接口的好处与实例操作
- 们无辜地对茂密的森林乱砍滥伐
- ORA-00845: MEMORY_TARGET not supported on this system
- 在C#中执行dos命令并返回结果字符串
- 数字电路的一些基本概念
- 以更深一层地揭示人物的精神世界
- 复数运算类
- linux中常用的几个命令技巧
- 大数据工程人员知识图谱