[size=medium]JavaScript 对象继承[/size]
几何形状结构继承图
[img]/upload/attachment/127179/f3d674d1-38aa-3c1d-97cd-a2209b69466c.jpg[/img]
几何形状结构继承图
[img]/upload/attachment/127179/f3d674d1-38aa-3c1d-97cd-a2209b69466c.jpg[/img]
Polygon = function(iSides) { //多边形
this.sides = iSides;
}
Polygon.prototype = {
getArea : function() { return 0; }
}
Triangle = function(iBase, iHeight) { //三角形
Polygon.call(this, 3);
this.base = iBase;
this.height = iHeight;
}
Triangle.prototype = new Polygon();
Triangle.prototype = {
getArea : function() {
return 0.5 * this.base * this.height;
}
}
Rectangle = function(iLength, iWidth) { //矩形
Polygon.call(this, 4);
this.length = iLength;
this.width = iWidth;
}
Rectangle.prototype = new Polygon();
Rectangle.prototype = {
getArea : function() {
return this.length * this.width;
}
}