Menu
CoddyTech

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

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

challenge icon

אתגר

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

המשימה שלך:

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

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

  2. הוסיפו מתודה calculateArea() ש:
    • מחשבת את השטח באמצעות הנוסחה: 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 אונליין