Menu
CoddyTech

오버라이딩과 Area 메서드

Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 56개 중 41번째.

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 컴파일러