如何在 Circle 之上实现 Shape
类?我的意思是 Circle 和 Rectangle 类应该继承自 Shape。
如果有人提供真实的代码,我会很高兴:)
这里我也用原型(prototype)定义创建了 Circle 类。
function Circle(radius){
this.radius = radius;
Circle.prototype.area = function(){return (Math.PI)* (Math.pow(this.radius,2));};
}
var circle1 = new Circle(5);
circle1.radius; //5
circle1.area() //78.53
最佳答案
您可以使用原型(prototype)在 JS 中实现继承,例如:
ChildClassName.prototype = new ParentClass();
在您的情况下定义形状类,然后像这样扩展它:
Circle.prototype = new Shape();
这将为您提供更多相关信息 :- http://phrogz.net/js/classes/OOPinJS2.html
关于javascript - 如何在 Circle 类之上实现 Shape 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023476/