오버라이딩과 Area 메서드
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 41번째.
챌린지
describe() 메서드를 오버라이딩하고 새로운 calculateArea() 메서드를 추가하여 Circle 클래스를 개선하세요.
작업 내용:
describe()메서드를 오버라이딩하여${super.describe()} (Circle with radius ${this.radius})를 반환하도록 하세요.예시: "A blue shape (Circle with radius 10)"
- 다음을 수행하는
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"