/*
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("头部已经创建好");
}
/*
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");
}
}
//接口类,用来创建接口
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']);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有