JavaScript接口的实现三种方式(推荐)
Javascript模仿接口可以有三种方式:1.注释法2.检查属性法3.鸭式辨形法
1.注释法:此方法属于程序文档范畴,对接口的继承实现完全依靠程序员自觉
/*
interfacePeople{
functioncreateHead();
functioncreateBody();
}
*/
varwoman=function(name){//implementsPeopleinterface
this.name=name;
}
woman.prototype.showName=function(){
alert(this.name);
}
woman.prototype.createBody=function(){//实现必要的方法
alert("身体已经创建好");
}
woman.prototype.createHead=function(){
alert("头部已经创建好");
}
//2.属性检查法:把要实现的接口方法添加到类属性列表里,通过定义好的检测反复检查是否已经实现了那些方法
//优缺点:可以强迫程序员实现接口,没实现就报错。不过虽然声明了自己实现了哪些方法,但实现时很可能有遗漏
/*
interfacePeople{
functioncreateHead();
functioncreateBody();
}
*/
varwoman=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("头部已经创建好");
}
functionimplement(obj,interfaces){
for(vari=1;i<interfaces.length;i++){
varinterfaceName=interfaces[i];
varinterfaceFound=false;
for(varj=0;j<obj.implementsInterfaces.length;j++){
if(obj.implementsInterfaces[j]=interfaceName){
interfaceFound=true;
break;
}
}
if(!interfaceFound){
returnfalse;
}
}
returntrue;
}
functionisImplememts(instance,interfaces){//判断对象是否已经继承相应接口
if(!implement(instance,interfaces)){
thrownewError("Objectdoesn'timplementarequiredinterface");
}
}
3.鸭式辨型法:(不通过外表判断鸭子,而通过其是否有鸭子的特性来判断。如JamesWhitcombRiley所说,像鸭子一样走路并且嘎嘎叫的就是鸭子)
上面俩种都声明了自己实现了那些接口,其实声明不重要,实现接口核心的是类实现了接口方法集。如果类具有了接口定义的所有方法函数名相同的函数,那么认为它实现了接口
//接口类,用来创建接口
varInterface=function(name,motheds){
if(agruments.length!=2){
thrownewError("Interfaceconstructorcalledwith"+arguments.length+"arguments,butexpectedexactly2");
}
this.name=name;
this.methods=[];
for(vari=0;i<motheds.length;i++){
if(typeofmotheds[i]!=='string'){
thrownewError('Interfaceconstructorexpectsmothednamestobe'+'passesinasastring');
}
this.methods.push(motheds[i]);
}
}
Interface.prototype.ensureImplements=function(objs){
if(agruments.length!=1){
thrownewError("Interfaceconstructorcalledwith"+arguments.length+"arguments,butexpectedexactly1")
}
for(vari=0;i<objs.length;i++){
varobj=objs[i];
for(varj=0;j<this.motheds.length;j++){
varmothed=this.methods[j];
if(!obj[mothed]||!typeofobj[mothed]!=='function'){
thrownewError('FunctionInterface.ensureImplements:implementsinterface'+this.name+',obj.mothed'+mothed+'wasnotfound');
}
}
}
}
//创建接口
varPeople=newInterface('People',['createHead','createBody']);
//子类
varWoman=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("女人头部已经创建好");
}
//子类
varMan=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']);