大学管理システム
CoddyのJavaScriptジャーニー「オブジェクト指向プログラミング」セクションの一部 — レッスン 55/56。
チャレンジ
このチャレンジでは、異なる役割と行動を持つさまざまな種類の人々(学生、教授)がいる大学システムを扱います。
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()); // "Hi, I'm Alice, a Computer Science student"
console.log(prof1.introduce()) // "Prof. Dr. Johnson from Mathematics"
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"