Menu
Coddy logo textTech

דריסה ומתודת שטח

חלק מהיחידה תכנות מונחה עצמים במסלול ה-JavaScript של Coddy. שיעור 41 מתוך 56.

challenge icon

אתגר

שפרו את המחלקה Circle באמצעות דריסה של המתודה describe() והוספת מתודה חדשה בשם calculateArea().

המשימה שלך:

  1. דרסו את המתודה <strong>describe()</strong> כך שתחזיר את ${super.describe()} (Circle with radius ${this.radius}).

    דוגמה: "A blue shape (Circle with radius 10)"

  2. הוסיפו מתודה בשם <strong>calculateArea()</strong> ש:
    • מחשבת את השטח באמצעות הנוסחה: Math.PI * this.radius * this.radius
    • מחזירה את השטח שחושב (אין צורך ב־console.log)

      *זוהי מתודה ייחודית ל־Circle (אינה נמצאת במחלקת האב Shape)

נסו בעצמכם

import { Shape } from './Shape.js';
import { Circle } from './Circle.js';

// בדיקות
const basicShape = new Shape('red');
console.log(basicShape.describe()); // "A red shape"

const myCircle = new Circle('blue', 10);
console.log(myCircle.describe()); // אמור להציג: "A blue shape (Circle with radius 10)"
console.log(myCircle.calculateArea()); // אמור להציג: "314.1592653589793"

כל השיעורים ביחידה תכנות מונחה עצמים

תרגלו בעצמכם: קומפיילר JavaScript אונליין