javascript面向对象编程(三)
作者:canque 来源:CanQue@RSSIDEA 发布时间:2008-03-07 13:19:00
标签:面向对象,编程,oop,javascript
阅读上一篇:javascript面向对象编程(二)
[Interface,Class.implement 接口及实现]
接口规定了一些方法,如果一个类实现了接口所定义的所有方法,就叫做实现了这个接口。诚然,javascript来模拟接口会带来一些效率上的损失,但是在大型项目特别是团队开发的时候,接口将带来很大的方便。使项目代码更加规范,更方便地查找错误信息。很多设计模式,像工厂模式、合成模式、装饰模式、命令模式等都依赖于接口来实现。
javascript模拟接口包括两部分:接口的模拟和接口实现的模拟。
[Interface接口类]
这段代码定义一个接口类,同样来自于《javascript design patterns》,略有改动。
// Constructor.
var Interface = function(name, methods){
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
"arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for (var i = 0, len = methods.length; i <len; i++) {
if (typeof methods[i] !== 'string') {
throw new Error("Interface constructor expects method names to be " +
"passed in as a string.");
}
this.methods.push(methods[i]);
}
};
[Interface.ensureImplements 接口实现检查]
这段代码检查一个对象是否实现了所要实现接口。
// Static class method.
Interface.ensureImplements = function(object){
if (arguments.length <2) {
throw new Error('Interface.ensureImplements:Wrong arguments number');
}
for (var i = 1, len = arguments.length; i <len; i++) {
var interfaces = arguments[i];
if (interfaces.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments " +
"two and above to be instances of Interface.");
}
for (var j = 0, methodLen = interfaces.methods.length; j <methodLen; j++) {
var method = interfaces.methods[j];
if (!object.prototype[method] || typeof(object.prototype[method]) !== 'function') {
throw new Error("Function Interface.ensureImplements: object " +
"does not implement the " +
interfaces.name +
" interface. Method " +
method +
" was not found.");
}
}
}
}
[Class.implement类实现接口]
接口定义后,还不能带来任何好处,需要在类中体现出来。看下面的两段代码就很清楚了:程序演示页面(3)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="JavaScript" src="http://rssidea.com/labs/OOP/OOP.js"></script>
<title>labs@RSSIDEA javascript OOP</title>
</head>
<body>
<script language="JavaScript">
//定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
var computer = new Interface('computer', ['motherboard', 'memory', 'cpu', 'harddisk'])
//定义一个PC类,实现computer接口
var PC = Class.create({
motherboardInfo: '',
memoryInfo: '',
cpuInfo: '',
harddiskInfo: '',
init: function(motherboard, memory, cpu, harddisk){
this.motherboardInfo = motherboard;
this.memoryInfo = memory;
this.cpuInfo = cpu;
this.harddiskInfo = harddisk;
},
motherboard: function(){
return this.motherboardInfo;
},
memory: function(){
return this.memoryInfo;
},
cpu: function(){
return this.cpuInfo;
},
mouse:function(){
return this.mouseInfo;
}
}).implement(computer);//浏览器报错,(没有硬盘电脑怎么转啊?)
var myComputer = new PC('微星主板', '现代2G', 'AMD3600+', '三星80G');
//输出电脑配置信息
trace(myComputer);
</script>
</body>
</html>
但是这段代码我们却得到一个浏览器错误,方法harddisk没有实现。(没有硬盘叫什么电脑啊?)
好吧,好吧!我承认是我拿买硬盘的钱去买了一个鼠标……(这鼠标不错吧?)
我原以为可以混过去,却被检查出来了。接口的工作和作用和上面的情况类似。现在,我需要再买个硬盘(harddisk)。程序演示页面(4)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="JavaScript" src="http://rssidea.com/labs/OOP/OOP.js"></script>
<title>labs@RSSIDEA javascript OOP</title>
</head>
<body>
<script language="JavaScript">
//定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
var computer = new Interface('computer', ['motherboard', 'memory', 'cpu', 'harddisk'])
//定义一个PC类,实现computer接口
var PC = Class.create({
motherboardInfo: '',
memoryInfo: '',
cpuInfo: '',
harddiskInfo: '',
init: function(motherboard, memory, cpu, harddisk){
this.motherboardInfo = motherboard;
this.memoryInfo = memory;
this.cpuInfo = cpu;
this.harddiskInfo = harddisk;
},
motherboard: function(){
return this.motherboardInfo;
},
memory: function(){
return this.memoryInfo;
},
cpu: function(){
return this.cpuInfo;
},
mouse:function(){
return this.mouseInfo;
},
harddisk:function(){
return this.harddiskInfo;
}//这是新买的硬盘
}).implement(computer);//实现了computer接口
var myComputer = new PC('微星主板', '现代2G', 'AMD3600+', '三星80G');
//输出电脑配置信息
trace(myComputer);
</script>
</body>
</html>
太好了!浏览器不再报错,这样,我们就实现了computer接口。


猜你喜欢
- 前端时间智能信息处理实训,我选择的课题为身份证号码识别,对中华人民共和国公民身份证进行识别,提取并识别其中的身份证号码,将身份证号码识别为字
- 1、使用del语句删除元素>>> i1 = ["a",'b','c',
- 新版本的selenium已经明确警告将不支持PhantomJS,建议使用headless的Chrome或FireFox。两者使用方式非常类似
- 本文实例讲述了GO语言常用的文件读取方式。分享给大家供大家参考。具体分析如下:Golang 的文件读取方法很多,刚上手时不知道怎么选择,所以
- create table [order] ( code varchar(50), createtime datetime ) --应用 us
- 方法一:def commaSpiltList(self, listData): listData = list(listData) strs
- 热词图很酷炫,也非常适合热点事件,抓住重点,以图文结合的方式表现出来,很有冲击力。下面这段代码是制作热词图的,用到了以下技术:jieba,把
- 合并两个数组 - concat()源代码:<!DOCTYPE html><html><body><
- 在进入一个页面的时候,一般在获取数据的同时,会先显示一个 loading ,等请求结束再隐藏 loading 渲染页面,只需要用一个属性去记
- 简介Button(按钮)组件用于实现各种各样的按钮。Button 组件可以包含文本或图像,你可以将一个 Python 的函数或方法与之相关联
- 导语:你不知道Python也能去除“背景”嘛?修饰图片中的头发是设计师最烦人的任务之一!要修得完美,
- 一、案例场景字段login_place,一共267725行记录,随机15条记录如下: 后续数据分析工作需要用到地理维度进行分析,所以需要把
- 网上学习的时候总会遇到一些好的文章,分享给大家,也谢谢作者的分享。Python 简介Python 是一个高层次的结合了解释性、编译性、互动性
- 今天在看罗素的《西方哲学史》时,忽然想到了这个想法,我认为可以从另外一个角度来看“用户体验“的影响因素。上面这个图是我今天思考的一部分,这是
- 算法复杂度分为时间复杂度和空间复杂度。其作用: 时间复杂度是指执行算法所需要的计算工作量; 而空间复杂度是指执行这个算法所需要的内存空间。
- 一、本节说明我们在开发过程中经常需要监听用户的输入,比如:用户的点击事件、拖拽事件、键盘事件等等。这就需要用到我们下面要学习的内容v-on指
- 本文实例为大家分享了python编写简单计算器的具体代码,供大家参考,具体内容如下做一个计算器,这是我们想要的效果。1、准备工作导入time
- 问题描述我有一个用于模型训练的DataFrame如下图所示:其中的country、province、city、county四列其实是位置信息
- 一、JS介绍  Javascript是一种由Netscape(网景)的LiveScript发展而来的原型
- 1、使用mysqldump工具将MySql数据库备份mysqldump -u root -p -c --default-character-