대학교 관리 시스템
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 55번째.
챌린지
이 챌린지에서는 서로 다른 역할과 행동을 가진 다양한 유형의 사람들(학생, 교수)이 있는 대학 시스템을 다룹니다.
1. Person 클래스 완성하기:
"Hi, I'm ${name}"를 반환하는introduce()메서드를 추가하세요.
2. Student 클래스 완성하기:
introduce()를 오버라이드하여${super.introduce()}, a ${this.major} student를 반환하도록 하세요. 예: "Hi, I'm Alice, a Computer Science student"- 0에서 100 사이의 성적을 받는
addGrade(grade)메서드를 추가하세요.- 유효성 검사: 성적이 0에서 100 사이인지 확인합니다.
- 유효한 경우:
this.grades배열에 추가합니다. - 유효하지 않은 경우:
"Grade must be between 0 and 100"를 로그로 출력합니다.
3. Professor 클래스 완성하기:
introduce()를 오버라이드하여“Prof. ${name} from ${department}”를 반환하도록 하세요. 예: “Prof. Smith from Computer Science”
직접 해보기
import { Person } from './person.js';
import { Student } from './student.js';
import { Professor } from './professor.js';
// 사람 생성
const student1 = new Student('Alice', 20, 'Computer Science');
const prof1 = new Professor('Dr. Johnson', 50, 'Mathematics');
console.log(student1.introduce()); // "안녕하세요, 저는 컴퓨터 공학 전공 학생인 Alice입니다"
console.log(prof1.introduce()) // "수학과의 Dr. Johnson 교수님"
student1.addGrade(85);
student1.addGrade(90);
student1.addGrade(95);
console.log(`${student1.name}'s GPA: ${student1.getGPA()}`); // ~3.6
student1.addGrade(105); // 출력되어야 함: "Grade must be between 0 and 100"