정적 Shape 카운터
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 42번째.
챌린지
정적 속성과 메서드를 사용하여 얼마나 많은 도형이 생성되었는지 추적하도록 Shape 클래스를 수정하세요.
수행 과제:
0으로 초기화된totalShapes라는 이름의 정적 속성을 추가하세요.- 생성자(constructor)에서 카운터를 증가시키세요:
Shape.totalShapes++; - 현재 전체 도형의 개수를 반환하는
getTotalCreated()라는 이름의 정적 메서드를 추가하세요.
직접 해보기
import { Shape } from './Shape.js';
import { Circle } from './Circle.js';
// 테스트
console.log(`Initial count: ${Shape.getTotalCreated()}`); // 0이어야 함
const shape1 = new Shape('red');
const shape2 = new Shape('green');
const circle1 = new Circle('blue', 5);
const circle2 = new Circle('yellow', 10);
console.log(`After creating 4 shapes: ${Shape.getTotalCreated()}`); // 4여야 함