javascriptMVC框架面向对象编程

    //抽象形状类
    $.Class("Shape", {}, {
        //构造函数
        init : function(edge) {
            this.edge = edge;
        },
        //实例方法
        getEdge : function() {
            return this.edge;
        },
        //实例方法
        calcArea : function() {
            return -1;
        }
    });
    
    //三角形类
    Shape("Triangle", {}, {
        //构造函数
        init : function(bottom, height) {
            //调用父类同名方法
            this._super(3);
            this.bottom = bottom;
            this.height = height;
        },
        //重写方法
        calcArea : function() {
            return this.bottom * this.height / 2;
        }
    });

    //矩形类
    Shape("Rectangle", {}, {
        //构造函数
        init : function(bottom, height) {
            //调用父类同名方法
            this._super(4);
            this.bottom = bottom;
            this.height = height;
        },
        //重写方法
        calcArea : function() {
            return this.bottom * this.height;
        }
    });

    var triangle = new Triangle(4, 5);
    console.log(triangle.getEdge());
    console.log(triangle.calcArea());

    var rectangle = new Rectangle(4, 5);
    console.log(rectangle.getEdge());
    console.log(rectangle.calcArea());
原文地址:https://www.cnblogs.com/zfc2201/p/3560424.html