首页 > 代码库 > 一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……

一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">

    var num = [88, 1151, 1];
    (function() {
        /**
         * 对于n1,n2,n3;如果其中任意两个分别乘以[1, 100]范围内的任意两个整数x,y的和加2或者减2等于
         * 另外第三个数,找出x和y.
         * @type {Number}
         */
        function MioFun () {
            this.a = arguments[0][0];
            this.b = arguments[0][1];
            this.c = arguments[0][2];
        }
        MioFun.prototype = {
            constructor : MioFun,
            findMax: function() {
                var temp = 0;
                if(this.a < this.b) {
                    temp = this.a;
                    this.a = this.b;
                    this.b = temp;
                }
                if(this.a < this.c) {
                    temp = this.a;
                    this.a = this.c;
                    this.c = temp;
                }
                return this;
            },
            calMain: function() {
                var i, j;
                if(this.a < this.b + this.c) return null;
                for(i = 1; i <= 100; i++) {
                    for(j = 1; j <= 100; j++) {
                        this.calFun(i, j);
                    }
                }
            },
            calFun: function(x, y) {
                if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {
                    console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));
                    console.log("////////////////////////////////////////////////////////////////////////////");
                }            
            }
        }
        var mio = new MioFun(arguments[0]);
        mio.findMax().calMain();        
    })(num);







    var arg = [88, 1152, 102];
    (function() {
        /**
         * 对于n1,n2,n3;如果其中任意两个分别乘以[1, 100]范围内的任意两个整数x,y的和加2或者减2等于
         * 另外第三个数,找出x和y.
         * @type {Number}
         */
        function SuperFun () { //父类成员属性
            if(arguments[0]) {
                this.a = arguments[0][0];
                this.b = arguments[0][1];
            }
        }
        SuperFun.prototype.findMax = function() { //父类成员方法findMax
            var temp = 0;
            if(this.a < this.b) {
                temp = this.a;
                this.a = this.b;
                this.b = temp;
            }
            if(this.a < this.c) {
                temp = this.a;
                this.a = this.c;
                this.c = temp;
            }
            return this;
        };
        SuperFun.prototype.calFun = function(x, y) { //父类成员方法calFun
            if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {
                console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));
                console.log("////////////////////////////////////////////////////////////////////////////");
            }            
        };
        function SubFun () {
            SuperFun.call(this, arguments[0]); //继承父类SuperFun属性
            this.c = arguments[0][2]; //子类伪私有成员属性
        }
        SubFun.prototype = new SuperFun();  //继承父类SuperFun方法
        SubFun.prototype.constructor = SubFun;
        SubFun.prototype.calMain =  function() { //子类伪私有成员方法
            var i, j;
            if(this.a < this.b + this.c) return null;
            for(i = 1; i <= 100; i++) {
                for(j = 1; j <= 100; j++) {
                    this.calFun(i, j);
                }
            }
        };       
        var mio = new SubFun(arguments[0]);
        mio.findMax().calMain();        
    })(arg);
	</script>
</head>
<body>
	
</body>
</html>

一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……