JS的继承和接口
来源:互联网 发布:js获取数组索引值 编辑:程序博客网 时间:2024/06/06 07:07
//Test接口var Person = E.createInterface('say', 'eat');var Chinese = function() {};Chinese.prototype.say = function() {alert('说汉语')}Chinese.prototype.eat = function() {alert('吃中餐')}E.implement(Chinese, Person);//Test继承var Instrument = function(name, tone) {this.name = name;this.tone = tone}Instrument.prototype.play = function() {alert(this.name + '的音色:' + this.tone);};var Guitar = function(name, tone) {this.superclass.call(this, name, tone);}Guitar = E.extend(Guitar, Instrument);var fingerGuitar = new Guitar('杉田健司', '高音甜中音稳低音狠 ');fingerGuitar.play();alert(fingerGuitar instanceof Guitar);
实现继承和接口的工具类
/** * */E = {};E.extend = function(sub, sup) {//借用构造函数sub.prototype = new sup();//保留父类的构造函数,以便在子类构造函数中用调用,将父类变量绑定在this下sub.prototype.superclass = sup;//因为重写了构造函数所以重新指定constructor,以使instanceof表现正常sub.prototype.constructor = sub;//因为已经将变量绑定到子类上,所以删除原型上多余的变量return sub;};//实现类是否实现了所有接口的方法E.implement = function() {var sub = arguments[0];for (var i = 1; i < arguments.length; i++) {var sup = arguments[i];try {sup.implemented(sub);} catch (e) {throw new Error('no implements interface ' + sup);}}};E.factory = {};//创建接口E.createInterface = E.factory.createInterface = function(methods) {var f = typeof arguments[0] === 'string';var p = f ? arguments : arguments[0];var len = p.length;var Interface = function() {};var _proto = Interface.prototype = {};_proto.implemented = function(constructor) {for (var i = 0; i < len; i++) {var obj = new constructor();if(!obj[p[i]]) {throw new Error('no implements interface');}}};return new Interface();};
0 0
- JS的继承和接口
- 关于js的接口和 继承实现的代码例子
- 接口继承和实现继承的区别
- 接口继承和实现继承的区别
- 接口和继承的区别
- 接口的组合和继承
- 继承和接口的区别
- 类的继承和接口
- 类的继承和接口
- java的继承和接口
- 继承和接口的设计
- JAVA的接口和继承
- 类的继承和接口的实现
- Java类的继承、接口的继承和实现
- BREW接口的继承和多态
- BREW接口的继承和多态
- BREW接口的继承和多态
- opp:接口和继承的差别
- 黑马程序员_java基础
- python参考手册 第四章
- datagridview绑定后添加行
- 个人通讯簿系统(两个项目,一个类库中有一个接口,一个类,另一个主窗体中 实现数据库的连接和动态查询)
- android persistent属性研究
- JS的继承和接口
- poj1236 Network of Schools
- java密码学学习整理--消息摘要与数字签名
- 上三角矩阵 元素 选取 组成一向量
- 巴伦 4分之一 平衡 不平衡
- 【Java编程】写入、读取、遍历Properties文件
- awk笔记
- 黑马程序员 IO 字符流中 的读取 和 写入
- percona XTRADB Cluster 5.6在ubuntu安装