정적 Shape 카운터
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 42번째.
챌린지
static properties and methods를 사용하여 생성된 도형의 개수를 추적하도록 Shape 클래스를 수정하세요.
작업 내용:
0으로 초기화된totalShapes라는 static property를 추가하세요.- constructor 안에서 카운터를 증가시키세요:
Shape.totalShapes++; - 전체 도형의 현재 개수를 반환하는
getTotalCreated()라는 static method를 추가하세요.
직접 해보기
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이어야 함