js实现接口的几种方式
Javascript模仿接口可以有三种方式:1.注释法 2.检查属性法 3.鸭式辨形法
1.注释法:此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){ //implements People interface
this.name = name;
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ //实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
}
//2.属性检查法:把要实现的接口方法添加到类属性列表里,通过定义好的检测反复检查是否已经实现了那些方法
//优缺点:可以强迫程序员实现接口,没实现就报错。不过虽然声明了自己实现了哪些方法,但实现时很可能有遗漏
/*
interface People{
function createHead();
function createBody();
}
*/
var woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
woman.prototype.showName = function(){
alert(this.name);
}
woman.prototype.createBody = function(){ //实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead = function(){
alert("头部已经创建好");
} function implement(obj,interfaces){
for(var i=1;i<interfaces.length;i++){
var interfaceName = interfaces[i];
var interfaceFound = false;
for(var j=0;j<obj.implementsInterfaces.length;j++){
if(obj.implementsInterfaces[j] = interfaceName){
interfaceFound = true;
break;
}
}
if(!interfaceFound){
return false;
}
}
return true;
} function isImplememts(instance,interfaces){ //判断对象是否已经继承相应接口
if(!implement(instance,interfaces)){
throw new Error("Object doesn't implement a required interface");
}
}
3.鸭式辨型法:(不通过外表判断鸭子,而通过其是否有鸭子的特性来判断。如James Whitcomb Riley所说,像鸭子一样走路并且嘎嘎叫的就是鸭子)
上面俩种都声明了自己实现了那些接口,其实声明不重要,实现接口核心的是类实现了接口方法集。如果类具有了接口定义的所有方法函数名相同的函数,那么认为它实现了接口
//接口类,用来创建接口
var Interface = function(name,motheds){
if(agruments.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;i<motheds.length;i++){
if(typeof motheds[i] !== 'string'){
throw new Error('Interface constructor expects mothed names to be'+'passes in as a string');
}
this.methods.push(motheds[i]);
}
}
Interface.prototype.ensureImplements = function(objs){
if(agruments.length != 1){
throw new Error("Interface constructor called with "+arguments.length+"arguments,but expected exactly 1")
}
for(var i=0;i<objs.length;i++){
var obj = objs[i];
for(var j=0;j<this.motheds.length;j++){
var mothed = this.methods[j];
if(!obj[mothed] || !typeof obj[mothed] !== 'function'){
throw new Error('Function Interface.ensureImplements:implements interface'+this.name+',obj.mothed'+mothed+'was not found');
}
}
}
}
//创建接口
var People = new Interface('People',['createHead','createBody']);
//子类
var Woman = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Woman.prototype.showName = function(){
alert(this.name);
}
Woman.prototype.createBody = function(){ //实现必要的方法
alert("女人身体已经创建好");
}
Woman.prototype.createHead = function(){
alert("女人头部已经创建好");
}
//子类
var Man = function(name){
this.name = name;
this.implementsInterfaces = ['People'];
}
Man.prototype.showName = function(){
alert(this.name);
}
Man.prototype.createBody = function(){ //实现必要的方法
alert("男人身体已经创建好");
}
Man.prototype.createHead = function(){
alert("男人头部已经创建好");
}
//判断是否实现
Poeple.ensureImplements(['Woman','Man']);
js实现接口的几种方式的更多相关文章
- JS对象创建的几种方式整理
本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...
- js实现继承的5种方式 (笔记)
js实现继承的5种方式 以下 均为 ES5 的写法: js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承机制,而是通过模仿实现的,根据js语言的本身的特性,js实现继承 ...
- 前端js,css文件合并三种方式,bat命令
前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...
- js获取时间戳的三种方式
js获取时间戳的三种方式 CreateTime--2018年5月23日08:44:10 Author:Marydon // 方式一:推荐使用 var timestamp=new Date().ge ...
- js 函数定义的2种方式
js 函数定义的2种方式 CreateTime--2018年3月29日18:36:14 Author:Marydon 方式一: /** * 函数式声明 */ function mode() { c ...
- Python调用API接口的几种方式 数据库 脚本
Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...
- js 复制文本的四种方式
js 复制文本的四种方式 一.总结 一句话总结:js文本复制主流方法:document的execCommand方法 二.js 复制文本的四种方式 纯 转载复制,非原创 原地址:http://www.c ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...
- js声明变量的三种方式
JS 声明变量的三种方式 (1)使用变量步骤:a.声明-->b.赋值-->3.调用 正确用法: <script type="text/javascript"> ...
随机推荐
- [SPOJ1557] Can you answer these queries II
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2482 [算法] 线段树维护历史最值 时间复杂度 : O(NlogN) [代码] #i ...
- cogs1070玻璃球游戏
1070. [焦作一中2012] 玻璃球游戏 ★ 输入文件:marbles.in 输出文件:marbles.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 小x ...
- 初学Java(一)
基本语法: 编写Java程序时,应注意以下几点: 1.大小写敏感:java是大小写敏感的,这就意味着标识符Hello与hello是不同的. 2.类名:对于所有的类来说,类名的首字母应该大写.如果类名由 ...
- 转:Ubuntu12.04编译VLC,在linux上运行
Ubuntu12.04编译vlc2.1.0 1.编译环境 VM8.0.1 # gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAP ...
- Cobbler安装配置简单使用
安装Cobbler [root@linux-node3 ~]# yum -y install epel-release [root@linux-node3 ~]# yum -y install cob ...
- 64位windows7下安装python,配置numpy和matplotlib库
一.Python的安装 1.下载python2.7,下载地址:http://www.python.org/,选择系统相应版本,我选择是的是python2.7.6 . python-2.7.6rc1.a ...
- 量子隐形传态1 Quantum Teleportation
量子隐形传态是量子纠缠的又一个应用. 隐形传态,所谓隐形的意思就是没有物质介质就传递了信息,在经典世界,传递信息要有介质,光.电磁波或者其他的什么,但是在量子的世界里,我可以把信息传递给你,并且不传递 ...
- Android 4.2开发环境搭建
一.工具 jdk1.7; eclipse 4.3(for java ee); Android SDK; 二.安装JDK并配置 安装略,配置如下: 右击 “我的电脑”->属性->高级系统设置 ...
- 如何设置Xcode模拟器地图的当前位置
使用模拟器上的地图的话,需要设置当前位置,开启定位后,才能定位准确. 一.选中模拟器:Debug - Location - Custom Location 弹出的纬经度坐标.纬经度.纬经度坐标,默认显 ...
- NITACMOJ144稳定串
点我>>题目链接 稳定串 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...