如何使用Record
==========

构造一个对象类型，其属性键为“Keys” ，其属性值为“Type”，可用于将一种类型的属性映射到另一种类型。

示例如下：

_// Define course types_
type Course = 'Math' | 'Chinese' | 'English';

_// Define grade types_
type Grade = number;

_// Define student grade record types_
type StudentGrades = Record<Course, Grade>;

_// Define the type of class grade record, where the key is the student ID and the value is the student's grade record_
type ClassGrades = Record<string, StudentGrades>;

interface StudentCourseGrade {
  Math: number,
  Chinese: number,
  English: number
}

let student1: StudentCourseGrade = {
  Math: 90,
  Chinese: 85,
  English: 92
}
let student2: StudentCourseGrade = {
  Math: 78,
  Chinese: 82,
  English: 85
}
let student3: StudentCourseGrade = {
  Math: 95,
  Chinese: 89,
  English: 90
}

_// Initialize class grades_
const classGrades: ClassGrades = {
  '001': student1,
  '002': student2,
  '003': student3
};

@Entry
@Component
struct Index {
 _ // Obtain the average grade of a student_
  getAverageGrade(studentId: string, grades: ClassGrades): number | null {
    const studentGrades = grades\[studentId\]; _// Obtain corresponding grade data for students_
    if (!studentGrades) {
      console.log(\`Student with ID ${studentId} not found.\`);
      return null;
    }

    const courses = Object.keys(studentGrades) as Course\[\]; _// Course type array_
   _ // Calculate the total grade of the course_
    const total = courses.reduce((sum, course) => sum + studentGrades\[course\], 0);
    return total / courses.length; _// Average score_
  }

  build() {
    Row() {
      Column() {
        Button('getAverageGrade')
          .onClick(() => {
           _ // output: 89_
            console.log('student average grade is:', this.getAverageGrade('001', classGrades));
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}