User-defined data types · 用户自定义数据类型
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| user-defined types/ˈjuːzə dɪˈfaɪnd taɪps/ | 用户定义类型 | yòng hù dìng yì lèi xíng |
| non-composite/nɒn ˈkɒmpəzɪt/ | 非组合 | fēi zǔ hé |
| composite/ˈkɒmpəzɪt/ | 组合 | zǔ hé |
| enumerated type/ɪˈnjuːməreɪtɪd taɪp/ | 枚举类型 | méi jǔ lèi xíng |
| pointer/ˈpɔɪntə/ | 指针 | zhǐ zhēn |
| dereference/ˌdiːˈrefrəns/ | 解引用 | jiě yǐn yòng |
| record/ˈrekɔːd/ | 记录 | jì lù |
| set/set/ | 集合 | jí hé |
| class/klæs/ | 类 | lèi |
| attributes/ˈætrɪbjuːts/ | 属性 | shǔ xìng |
| methods/ˈmeθədz/ | 方法 | fāng fǎ |
A type that makes the mistake impossible
- A hospital system stored a patient's blood group as a
STRING. Someone typed "0+" instead of "O+": a zero for a letter. The field accepted it, because a string accepts anything. - Nothing in the program could tell that "0+" was not a blood group, so the error travelled untouched to the point where it mattered.
- Give the field a type whose values are exactly the eight blood groups, and the assignment is rejected the moment it is written, by the compiler, before the program ever runs.
- That is what user-defined types 用户定义类型 buy you. This lesson is the ones the syllabus names, and how to choose between them.
让错误不可能发生的类型
- 一个医院系统把病人的血型存成
STRING。有人把 "O+" 打成了 "0+":用零代替了字母。字段接受了它,因为字符串什么都接受。 - 程序里没有任何东西能判断 "0+" 不是血型,所以这个错误原封不动地一路传到它要紧的地方。
- 给这个字段一个取值恰好是八种血型的类型,那么这次赋值在写下的一刻就会被编译器拒绝,程序根本还没运行。
- 这就是用户定义类型(user-defined types)带来的东西。这一课讲大纲点名的几种类型,以及怎样在它们之间选择。
Why user-defined types are needed
- A built-in
STRINGlets you store nonsense in a field that should hold one of a few legal values; a user-defined type restricts it, so the compiler catches more mistakes. - Real entities are usually a collection of values of different types, and a built-in type can hold only one.
DECLARE MyTaxi : Vehicleis self-documenting: it says what the variable is, whereDECLARE MyTaxi : STRINGsays only how it is stored.
为什么需要用户定义类型
- 内置的
STRING允许你在只应保存几个合法值的字段里存进无意义的内容;用户定义类型限制它,于是编译器能抓到更多错误。 - 真实实体通常是不同类型的值的集合,而内置类型只能保存一个。
DECLARE MyTaxi : Vehicle是自解释的:它说出变量是什么,而DECLARE MyTaxi : STRING只说它怎样存储。
Why are user-defined types needed? Select all · 所有 that apply. · 为什么需要用户自定义类型?选择所有适用的选项。
Restriction, grouping and self-documentation. Speed is not the reason; correctness and clarity are. · 限制、分组和自我文档化。速度不是原因;正确性和清晰度才是。
Non-composite types: enumerated
- A non-composite 非组合 type holds a single value; a composite 组合 type groups several values together.
- An enumerated type 枚举类型 has values that are a fixed list of named constants. The names are values of the new type, stored internally as small integers.
- Uses: days of the week, colours, status codes, blood groups: any field whose legal values are a short known list.
Six named values, and nothing else is assignable
非组合类型:枚举
- 非组合(non-composite)类型保存单个值;组合(composite)类型把几个值组在一起。
- 枚举类型(enumerated type)的取值是一个固定的命名常量列表。这些名字是新类型的值,内部存成小整数。
TYPE Vehicle = (M100, M230, T101, T102, T120, T150)
DECLARE MyTaxi : Vehicle
MyTaxi ← T102 // 列表之外的任何东西都被拒绝
- 用途:星期、颜色、状态码、血型:任何合法值是一个短的已知列表的字段。

六个命名值,其他任何东西都不能赋进来
An enumerated type holds: · 枚举类型包含:
An enumerated type restricts a variable to one of a fixed set of named values (e.g. days of the week). · 枚举类型将变量限制为固定命名值集合中的一个(例如一周中的几天)。
Non-composite types: pointer
- A pointer 指针 holds the memory address of another variable, or
NULLmeaning "no target". - To dereference 解引用 a pointer, written
p^, is to reach the variable it points at.
- Pointers are what make dynamic structures possible: a linked list is nodes joined by pointers, and a tree is nodes with two. They also pass a large record to a procedure without copying it.
The variable holds where the data is, not the data
非组合类型:指针
- 指针(pointer)保存另一个变量的内存地址,或表示"没有目标"的
NULL。 - 对指针解引用(dereference),写作
p^,就是访问它指向的那个变量。
TYPE PNode = ^TNode // 指向 TNode 的指针
DECLARE p : PNode
p ← NEW TNode
p^.Value ← 42 // 先解引用,再访问字段
- 指针让动态结构成为可能:链表是由指针连起来的节点,树是每个节点带两个指针。它们还能把一条大记录传给过程而不复制它。

变量保存的是数据在哪里,而不是数据
A pointer stores the memory address of another variable, and dereferencing it (e.g. p^) reaches the variable it points to. · 指针存储另一个变量的内存地址,解引用它(例如 p^)即可访问其所指向的变量。
Pointers (with NULL marking "points to nothing") are how dynamic structures like linked lists and trees are built. · 指针(用 NULL 标记“指向空”)是构建链表和树等动态结构的方式。
Reaching the variable a pointer points at, written p^, is called ____. · 访问指针所指向的变量,写作 p^,称为 ____。
The pointer holds an address; dereferencing follows it to the data. That is how linked lists and trees are built. · 指针持有地址;解引用跟随它到达数据。这就是构建链表和树的方式。
Composite types
- A record 记录 groups fields of different types under one name, in a
TYPE … ENDTYPEblock. You met it in topic 10. - A set 集合 is an unordered collection of unique values, with the operations add, remove, membership test, union and intersection.
- A class 类 and its instances, objects, combine data fields, the attributes 属性, with the operations on them, the methods 方法.
Fields of different types, one identifier
组合类型
- 记录(record)在一个名字下把不同类型的字段组在一起,写在
TYPE … ENDTYPE块里。你在第 10 单元见过它。 - 集合(set)是无序的、元素唯一的集合,操作有添加、删除、成员测试、并集和交集。
DECLARE Available : SET OF Colour
Available ← {Red, Blue}
IF Green IN Available THEN
…
ENDIF
- 类(class)和它的实例对象,把数据字段——属性(attributes)——与作用于它们的操作——方法(methods)——结合起来。

不同类型的字段,一个标识符
Match each user-defined type to what it holds. · 将每个用户自定义类型与其包含的内容匹配。
Enumerated = fixed named values; pointer = an address; class = data + behaviour; set = unique unordered values. · 枚举 = 固定的命名值;指针 = 一个地址;类 = 数据 + 行为;集合 = 无序的唯一值。
A set is an ordered collection in which a value may appear more than once. · 集合是一个有序集合,其中值可能出现多次。
A set is unordered and its values are unique. An array is the ordered structure that allows repeats. · 集合是无序的且其值唯一。数组是允许重复的有序结构。
The class
- A class is the definition; an object is an instance of it, one actual taxi with its own capacity.
- What distinguishes it from a record is the methods: a record is data alone, a class is data and the behaviour that belongs with it.
类
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
- 类是定义;对象是它的一个实例,一辆有自己容量的实际出租车。
- 把它与记录区分开的是方法:记录只有数据,类是数据加上与之相伴的行为。
A class combines: · 类结合:
A class bundles attributes and methods; an object is an instance of a class. · 类捆绑属性和方法;对象是类的一个实例。
What distinguishes a class from a record? · 什么区分了类和记录?
Both group fields; only the class carries behaviour. An object is one instance of the class. · 两者都分组字段;只有类携带行为。对象是类的一个实例。
Worked example: choose the type and justify it
- A booking system stores, for each booking, a status that must be one of Requested, Confirmed or Cancelled. Which user-defined type, and why?
- An enumerated type: the legal values are a short fixed list, so the type prevents any other value being assigned and the code reads as the domain does.
- And for the booking itself: a reference, a date, a passenger count and a price? A record, because the values are of different types and always describe one booking together.
- And for the set of seats still free? A set: unordered, no duplicates, and membership is the operation you need.
例题:选择类型并论证
- 一个预订系统为每笔预订存储一个状态,必须是 Requested、Confirmed 或 Cancelled 之一。用哪种用户定义类型,为什么?
- 枚举类型:合法值是一个短的固定列表,所以该类型阻止任何其他值被赋入,代码读起来也和业务领域一致。
- 那预订本身呢:一个编号、一个日期、一个乘客数和一个价格? 记录,因为这些值类型不同,而且总是共同描述一笔预订。
- 那仍然空着的座位集合呢? 集合:无序、无重复,而成员测试正是你需要的操作。
Choosing between them
| Need | Type |
|---|---|
| a value from a short fixed list | enumerated |
| indirection, or a structure that grows | pointer |
| several fields of different types describing one thing | record |
| an unordered collection of unique values | set |
| state and the behaviour that acts on it | class |
在它们之间选择
| 需求 | 类型 |
|---|---|
| 来自一个短固定列表的值 | 枚举 |
| 间接引用,或会增长的结构 | 指针 |
| 共同描述一个事物的、类型不同的若干字段 | 记录 |
| 无序且元素唯一的集合 | 集合 |
| 状态加上作用于它的行为 | 类 |
Programming concept lab · 编程概念实验室
Connect examples to the programming idea they show. · 将示例与其展示的编程概念连接起来。
For a variable that must be one of {Red, Green, Blue}, the best type is: · 对于必须是 {Red, Green, Blue} 之一的变量,最佳类型是:
A fixed list of allowed values is exactly what an enumerated type is for. · 允许的值的固定列表正是枚举类型的用途。
Marks that slip away
- An enumerated type's values are named constants, not strings.
MyTaxi ← "T102"is a different thing and is wrong. - A pointer holds an address, not the value. Dereferencing is the step that reaches the value.
- A set is unordered with no duplicates; an array is ordered and may repeat. The question's wording tells you which.
- What makes a class more than a record is its methods. Say that when asked for the difference.
容易丢掉的分
- 枚举类型的取值是命名常量,不是字符串。
MyTaxi ← "T102"是另一回事,是错的。 - 指针保存地址,不是值。解引用才是访问值的那一步。
- 集合无序、无重复;数组有序、可重复。题目的措辞会告诉你要哪一个。
- 让类不只是记录的是它的方法。被问区别时要说这一点。
You've got it
- user-defined types restrict what can be stored, group values that belong together, and make code self-documenting
- non-composite: enumerated (a fixed list of named constants) and pointer (an address, reached by dereferencing)
- composite: record (fields of different types), set (unordered, unique), class/object (attributes plus methods)
- choose from the need, and justify with the property that fits: a fixed list, indirection, mixed fields, uniqueness, or state with behaviour
你掌握了
- 用户定义类型限制可存储的内容、把属于一起的值组在一起,并让代码自解释
- 非组合:枚举(固定的命名常量列表)和指针(地址,通过解引用访问)
- 组合:记录(不同类型的字段)、集合(无序、唯一)、类/对象(属性加方法)
- 从需求出发选择,并用契合的性质论证:固定列表、间接引用、混合字段、唯一性,或状态带行为