javascript面向对象编程(三)

Published by canque on February 26, 2008 11:43 pm under [web发展] Tags: ,

[Interface,Class.implement 接口及实现]
接口规定了一些方法,如果一个类实现了接口所定义的所有方法,就叫做实现了这个接口。诚然,javascript来模拟接口会带来一些效率上的损失,但是在大型项目特别是团队开发的时候,接口将带来很大的方便。使项目代码更加规范,更方便地查找错误信息。很多设计模式,像工厂模式、合成模式、装饰模式、命令模式等都依赖于接口来实现。
javascript模拟接口包括两部分:接口的模拟和接口实现的模拟。

[Interface接口类]
这段代码定义一个接口类,同样来自于《javascript design patterns》,略有改动。

Actionscript:
  1. // Constructor.
  2.     var Interface = function ( name, methods ) {
  3.         if ( arguments. length != 2 ) {
  4.             throw new Error ( "Interface constructor called with " + arguments. length +
  5.             "arguments, but expected exactly 2." );
  6.         }
  7.         this. name = name;
  8.         this. methods = [ ];
  9.         for ( var i = 0, len = methods. length; i <len; i++ ) {
  10.             if ( typeof methods [i ] !== 'string' ) {
  11.                 throw new Error ( "Interface constructor expects method names to be " +
  12.                 "passed in as a string." );
  13.             }
  14.             this. methods. push (methods [i ] );
  15.         }
  16.     };

[Interface.ensureImplements 接口实现检查]
这段代码检查一个对象是否实现了所要实现接口。

Actionscript:
  1. // Static class method.
  2.     Interface. ensureImplements = function ( object ) {
  3.         if ( arguments. length < 2 ) {
  4.             throw new Error ( 'Interface.ensureImplements:Wrong arguments number' );
  5.         }
  6.         for ( var i = 1, len = arguments. length; i <len; i++ ) {
  7.             var interfaces = arguments [i ];
  8.             if (interfaces. constructor !== Interface ) {
  9.                 throw new Error ( "Function Interface.ensureImplements expects arguments " +
  10.                 "two and above to be instances of Interface." );
  11.             }
  12.             for ( var j = 0, methodLen = interfaces. methods. length; j <methodLen; j++ ) {
  13.                 var method = interfaces. methods [j ];
  14.                 if (! object. prototype [method ] || typeof ( object. prototype [method ] ) !== 'function' ) {
  15.                     throw new Error ( "Function Interface.ensureImplements: object " +
  16.                     "does not implement the " +
  17.                     interfaces. name +
  18.                     " interface. Method " +
  19.                     method +
  20.                     " was not found." );
  21.                 }
  22.             }
  23.         }
  24.     }

[Class.implement类实现接口]
接口定义后,还不能带来任何好处,需要在类中体现出来。看下面的两段代码就很清楚了:程序演示页面(3)

JavaScript:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns= "http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8"/>
  5. <script language= "JavaScript" src= "http://rssidea.com/labs/OOP/OOP.js"></script>
  6. <title>labs@RSSIDEA javascript OOP</title>
  7. </head>
  8. <body>
  9. <script language= "JavaScript">
  10.     //定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
  11.     var computer = new Interface ( 'computer', [ 'motherboard', 'memory', 'cpu', 'harddisk' ] )
  12.     //定义一个PC类,实现computer接口
  13.     var PC = Class. create ( {
  14.         motherboardInfo: '',
  15.         memoryInfo: '',
  16.         cpuInfo: '',
  17.         harddiskInfo: '',
  18.         init: function (motherboard, memory, cpu, harddisk ) {
  19.             this. motherboardInfo = motherboard;
  20.             this. memoryInfo = memory;
  21.             this. cpuInfo = cpu;
  22.             this. harddiskInfo = harddisk;
  23.         },
  24.         motherboard: function ( ) {
  25.             return this. motherboardInfo;
  26.         },
  27.         memory: function ( ) {
  28.             return this. memoryInfo;
  29.         },
  30.         cpu: function ( ) {
  31.             return this. cpuInfo;
  32.         },
  33.         mouse: function ( ) {
  34.             return this. mouseInfo;
  35.         }
  36.     } ). implement (computer ); //浏览器报错,(没有硬盘电脑怎么转啊?)
  37.     var myComputer = new PC ( '微星主板', '现代2G', 'AMD3600+', '三星80G' );
  38.     //输出电脑配置信息
  39.     trace (myComputer );
  40. </script>
  41. </body>
  42. </html>

但是这段代码我们却得到一个浏览器错误,方法harddisk没有实现。(没有硬盘叫什么电脑啊?)
好吧,好吧!我承认是我拿买硬盘的钱去买了一个鼠标……(这鼠标不错吧?)

我原以为可以混过去,却被检查出来了。接口的工作和作用和上面的情况类似。现在,我需要再买个硬盘(harddisk)。程序演示页面(4)

JavaScript:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns= "http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8"/>
  5. <script language= "JavaScript" src= "http://rssidea.com/labs/OOP/OOP.js"></script>
  6. <title>labs@RSSIDEA javascript OOP</title>
  7. </head>
  8. <body>
  9. <script language= "JavaScript">
  10.     //定义一个computer的接口,要求实现motherborad,memory,cpu,harddisk的方法。
  11.     var computer = new Interface ( 'computer', [ 'motherboard', 'memory', 'cpu', 'harddisk' ] )
  12.     //定义一个PC类,实现computer接口
  13.     var PC = Class. create ( {
  14.         motherboardInfo: '',
  15.         memoryInfo: '',
  16.         cpuInfo: '',
  17.         harddiskInfo: '',
  18.         init: function (motherboard, memory, cpu, harddisk ) {
  19.             this. motherboardInfo = motherboard;
  20.             this. memoryInfo = memory;
  21.             this. cpuInfo = cpu;
  22.             this. harddiskInfo = harddisk;
  23.         },
  24.         motherboard: function ( ) {
  25.             return this. motherboardInfo;
  26.         },
  27.         memory: function ( ) {
  28.             return this. memoryInfo;
  29.         },
  30.         cpu: function ( ) {
  31.             return this. cpuInfo;
  32.         },
  33.         mouse: function ( ) {
  34.             return this. mouseInfo;
  35.         },
  36.         harddisk: function ( ) {
  37.             return this. harddiskInfo;
  38.         } //这是新买的硬盘
  39.     } ). implement (computer ); //实现了computer接口
  40.     var myComputer = new PC ( '微星主板', '现代2G', 'AMD3600+', '三星80G' );
  41.     //输出电脑配置信息
  42.     trace (myComputer );
  43. </script>
  44. </body>
  45. </html>

太好了!浏览器不再报错,这样,我们就实现了computer接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值