我轉(zhuǎn)載的啊:
function Interface(name, methods)
{
if(arguments.length != 2) {
throw new Error("接口構(gòu)造函數(shù)含" + arguments.length + "個(gè)參數(shù), 但需要2個(gè)參數(shù).");
}
this.name = name;
this.methods = [];
if(methods.length < 1) {
throw new Error("第二個(gè)參數(shù)為空數(shù)組.");
}
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i][0] !== 'string') {
throw new Error("接口構(gòu)造函數(shù)第一個(gè)參數(shù)必須為字符串類型.");
}
if(methods[i][1] && typeof methods[i][1] !== 'number') {
throw new Error("接口構(gòu)造函數(shù)第二個(gè)參數(shù)必須為整數(shù)類型.");
}
if(methods[i].length == 1) {
methods[i][1] = 0; // 只有一個(gè)方法名稱,沒有參數(shù),所以……
}
this.methods.push(methods[i]);
}???
};
Interface.registerImplements = function(object) {
if(arguments.length < 2) {
throw new Error("接口的實(shí)現(xiàn)必須包含至少2個(gè)參數(shù)."); // 類名和方法數(shù)組
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("從第2個(gè)以上的參數(shù)必須為接口實(shí)例.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j][0];
if(!object[method] || typeof object[method] !== 'function' || object[method].getParameters().length != interface.methods[j][1]) {
throw new Error("接口的實(shí)現(xiàn)對(duì)象不能執(zhí)行" + interface.name + "的接口方法" + method + ",因?yàn)樗也坏交蛘卟黄ヅ?");
}
}
}
};
Function.prototype.getParameters = function() {
var str = this.toString();
var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/\s*/g,'');???? //取得參數(shù)字符串
try
{
return (paramString.length == 0 ? [] : paramString.split(','));
}
catch(err)
{
throw new Error("函數(shù)不合法!");
}
}
var Person = new Interface("Person", [["getName", 0], ["setName", 1]]);
function Man()
{
this.name = "";
Interface.registerImplements(this, Person);
}
Man.prototype.getName = function() {
return this.name;
};
Man.prototype.setName = function(name) {
this.name = name;
};
var man = new Man();
??? man.setName("Leepy");
??? alert(man.getName());
function SchoolBoy(classNo, post)
{
Man.call(this);
this._chassNo = classNo;
this._post = post;
}
SchoolBoy.prototype = new Man();
SchoolBoy.prototype.getName = function() {
return "Mr " + this.name;
}
SchoolBoy.prototype.setName = function(name) {
this.name = name + "'s";
}
var schoolboy = new SchoolBoy("三年二班", "班長(zhǎng)");
schoolboy.setName("周杰倫");
alert(schoolboy.getName());
function Interface(name, methods)
{
if(arguments.length != 2) {
throw new Error("接口構(gòu)造函數(shù)含" + arguments.length + "個(gè)參數(shù), 但需要2個(gè)參數(shù).");
}
this.name = name;
this.methods = [];
if(methods.length < 1) {
throw new Error("第二個(gè)參數(shù)為空數(shù)組.");
}
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i][0] !== 'string') {
throw new Error("接口構(gòu)造函數(shù)第一個(gè)參數(shù)必須為字符串類型.");
}
if(methods[i][1] && typeof methods[i][1] !== 'number') {
throw new Error("接口構(gòu)造函數(shù)第二個(gè)參數(shù)必須為整數(shù)類型.");
}
if(methods[i].length == 1) {
methods[i][1] = 0; // 只有一個(gè)方法名稱,沒有參數(shù),所以……
}
this.methods.push(methods[i]);
}???
};
Interface.registerImplements = function(object) {
if(arguments.length < 2) {
throw new Error("接口的實(shí)現(xiàn)必須包含至少2個(gè)參數(shù)."); // 類名和方法數(shù)組
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("從第2個(gè)以上的參數(shù)必須為接口實(shí)例.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j][0];
if(!object[method] || typeof object[method] !== 'function' || object[method].getParameters().length != interface.methods[j][1]) {
throw new Error("接口的實(shí)現(xiàn)對(duì)象不能執(zhí)行" + interface.name + "的接口方法" + method + ",因?yàn)樗也坏交蛘卟黄ヅ?");
}
}
}
};
Function.prototype.getParameters = function() {
var str = this.toString();
var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/\s*/g,'');???? //取得參數(shù)字符串
try
{
return (paramString.length == 0 ? [] : paramString.split(','));
}
catch(err)
{
throw new Error("函數(shù)不合法!");
}
}
var Person = new Interface("Person", [["getName", 0], ["setName", 1]]);
function Man()
{
this.name = "";
Interface.registerImplements(this, Person);
}
Man.prototype.getName = function() {
return this.name;
};
Man.prototype.setName = function(name) {
this.name = name;
};
var man = new Man();
??? man.setName("Leepy");
??? alert(man.getName());
function SchoolBoy(classNo, post)
{
Man.call(this);
this._chassNo = classNo;
this._post = post;
}
SchoolBoy.prototype = new Man();
SchoolBoy.prototype.getName = function() {
return "Mr " + this.name;
}
SchoolBoy.prototype.setName = function(name) {
this.name = name + "'s";
}
var schoolboy = new SchoolBoy("三年二班", "班長(zhǎng)");
schoolboy.setName("周杰倫");
alert(schoolboy.getName());

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
