我轉載的啊:
function Interface(name, methods)
{
if(arguments.length != 2) {
throw new Error("接口構造函數含" + arguments.length + "個參數, 但需要2個參數.");
}
this.name = name;
this.methods = [];
if(methods.length < 1) {
throw new Error("第二個參數為空數組.");
}
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i][0] !== 'string') {
throw new Error("接口構造函數第一個參數必須為字符串類型.");
}
if(methods[i][1] && typeof methods[i][1] !== 'number') {
throw new Error("接口構造函數第二個參數必須為整數類型.");
}
if(methods[i].length == 1) {
methods[i][1] = 0; // 只有一個方法名稱,沒有參數,所以……
}
this.methods.push(methods[i]);
}???
};
Interface.registerImplements = function(object) {
if(arguments.length < 2) {
throw new Error("接口的實現必須包含至少2個參數."); // 類名和方法數組
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("從第2個以上的參數必須為接口實例.");
}
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("接口的實現對象不能執行" + interface.name + "的接口方法" + method + ",因為它找不到或者不匹配.");
}
}
}
};
Function.prototype.getParameters = function() {
var str = this.toString();
var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/\s*/g,'');???? //取得參數字符串
try
{
return (paramString.length == 0 ? [] : paramString.split(','));
}
catch(err)
{
throw new Error("函數不合法!");
}
}
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("三年二班", "班長");
schoolboy.setName("周杰倫");
alert(schoolboy.getName());
function Interface(name, methods)
{
if(arguments.length != 2) {
throw new Error("接口構造函數含" + arguments.length + "個參數, 但需要2個參數.");
}
this.name = name;
this.methods = [];
if(methods.length < 1) {
throw new Error("第二個參數為空數組.");
}
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i][0] !== 'string') {
throw new Error("接口構造函數第一個參數必須為字符串類型.");
}
if(methods[i][1] && typeof methods[i][1] !== 'number') {
throw new Error("接口構造函數第二個參數必須為整數類型.");
}
if(methods[i].length == 1) {
methods[i][1] = 0; // 只有一個方法名稱,沒有參數,所以……
}
this.methods.push(methods[i]);
}???
};
Interface.registerImplements = function(object) {
if(arguments.length < 2) {
throw new Error("接口的實現必須包含至少2個參數."); // 類名和方法數組
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.constructor !== Interface) {
throw new Error("從第2個以上的參數必須為接口實例.");
}
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("接口的實現對象不能執行" + interface.name + "的接口方法" + method + ",因為它找不到或者不匹配.");
}
}
}
};
Function.prototype.getParameters = function() {
var str = this.toString();
var paramString = str.slice(str.indexOf('(') + 1, str.indexOf(')')).replace(/\s*/g,'');???? //取得參數字符串
try
{
return (paramString.length == 0 ? [] : paramString.split(','));
}
catch(err)
{
throw new Error("函數不合法!");
}
}
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("三年二班", "班長");
schoolboy.setName("周杰倫");
alert(schoolboy.getName());

更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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