1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| class Student extends Person { constructor(name, age, studentId, major) { super(name, age); this.studentId = studentId; this.major = major; this.grades = []; } sayHello() { return `Hello, I'm ${this.name}, a ${this.major} student`; } addGrade(grade) { this.grades.push(grade); } getGPA() { if (this.grades.length === 0) return 0; const sum = this.grades.reduce((total, grade) => total + grade, 0); return (sum / this.grades.length).toFixed(2); } static createTransferStudent(name, age, studentId, major, previousSchool) { const student = new Student(name, age, studentId, major); student.previousSchool = previousSchool; return student; } }
const student = new Student('Alice', 20, 'S12345', 'Computer Science'); student.addGrade(85); student.addGrade(92); console.log(student.getGPA());
|