• 收集 关于js的接口和 继承实现的代码例子


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> 关于js的接口和 继承实现的代码例子 </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <script>
      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("函数不合法!");
        }
    }

    </script>
    </HEAD>

    <BODY>
    <script type="text/javascript">
    var Person = new Interface("Person", [["getName", 0], ["setName", 1]]);
    function Man()
    {
        Interface.registerImplements(this, Person);
    }
    Man.prototype.name='';
    Man.prototype.getName = function() {
        return this.name;
    };
    Man.prototype.setName = function(name) {
        this.name = name;
    };
    function test()
    {
        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("周杰伦");

    function jic()
    {
      alert(schoolboy.getName());
    }
    </script>

    <input type="button" value="js接口的实现" onclick="test();" />
    <br>
    <input type="button" value="js的继承" onclick="jic()" />
    </BODY>
    </HTML>

  • 相关阅读:
    ASP.NET MVC路由模块
    线程安全的单例模式
    MVC自带表单效验
    MSsql 中 in 语法排序的说明
    Web.Config配置错误页面处理
    WCF基本应用
    .NET微信自定义分享标题、缩略图、超链接及描述的设置方法
    .NET微信通过授权获取用户的基本信息
    C#中获取服务器IP,客户端IP以及网卡物理地址
    .NET获取客户端、服务器端的信息
  • 原文地址:https://www.cnblogs.com/litao229/p/1394461.html
Copyright © 2020-2023  润新知