Records · 记录
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Different types under one name
- AS topic 10.1 asks you to define a record: one identifier holding fields of different types.
TYPE…ENDTYPEnames the record and declares each field.- This is not an A2 class. There are no methods here.
中文
一个名字下面放不同类型
- AS 主题 10.1 要求你定义记录:一个标识符,里面是不同类型的字段。
TYPE…ENDTYPE给记录命名,并声明每个字段。- 这不是 A2 的类。这里没有方法。
TYPE Student
DECLARE Name : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE S : Student
S.Name ← "Ada"
S.Mark ← 91
OUTPUT S.Name & " " & S.Mark
English
A field is read with a dot
S.Nameis theNamefield ofS.- You may copy a whole record:
Pupil2 ← Pupil1copies every field. - An array of records uses one index, then the field:
Form[1].Mark.
中文
用点号读取字段
S.Name是S的Name字段。- 可以复制整条记录:
Pupil2 ← Pupil1会复制每一个字段。 - 记录数组先写下标,再写字段:
Form[1].Mark。
TYPE Student
DECLARE Name : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE Form : ARRAY[1:2] OF Student
Form[1].Name ← "Ada"
Form[1].Mark ← 91
Form[2].Name ← "Bo"
Form[2].Mark ← 80
OUTPUT Form[2].Name
English
Watch out
- Declare the
TYPEbefore youDECLAREa variable of that type. - Enumerated types and pointers are A2 (topic 13.1). They are not in this AS course.
中文
注意
- 先声明
TYPE,再DECLARE这个类型的变量。 - 枚举类型和指针是 A2(主题 13.1)。它们不在这门 AS 课里。
English
Common mistakes
- Each field has its own
DECLAREinside theTYPE. ENDTYPEcloses the definition. Forgetting it leaves the type unfinished.
中文
常见错误
TYPE里面的每个字段都有自己的DECLARE。ENDTYPE结束定义。漏掉它,这个类型就没有写完。
English
Now you try
- Define a record, store two fields, and output them.
中文
现在你来试
- 定义一条记录,存入两个字段,然后输出它们。
Define a Student record with Name and Mark. Store Ada and 91, and output Ada 91. · 定义带 Name 和 Mark 的 Student 记录。存入 Ada 和 91,输出 Ada 91。
Click Run to see the output here. · 点击“运行”查看此处输出。
An array Form of two students is partly filled below. Store Bo as the second name and output it. · 下面的 Form 数组有两名学生,只填了一部分。把第二名的名字存为 Bo 并输出它。
Click Run to see the output here. · 点击“运行”查看此处输出。
The array Form of two students is filled in below. Add up Form[1].Mark and Form[2].Mark with a FOR loop and output the total (171). · 下面的 Form 数组已填好两名学生。用 FOR 循环把 Form[1].Mark 和 Form[2].Mark 相加,并输出总和(171)。
Click Run to see the output here. · 点击“运行”查看此处输出。