User-defined data types
導入済み
15:07
ユーザー定義データ型
単純な文字列フィールドは、意味のない値でも喜んで格納します。「車両タイプ」を求め、誰かが「バナナ」と入力しても、プログラムは異議を唱えずに受け入れます。しかし、あなたが…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| field/fiːld/ | A named component of a record, with its own value and type. |
| enumerated type/ɪˈnjuːməreɪtɪd taɪp/ | 列挙型 |
| enum literal | A named value in the fixed list of an enumerated type. |
| compiler/kəmˈpaɪlə/ | Software that translates a program from a source language into another form, commonly machine or intermediate code. |
| run time/rʌn taɪm/ | The period during which a program is running on a computer. |
| assigned | Given a value to store in a variable or field. |
| user-defined types/ˈjuːzə dɪˈfaɪnd taɪps/ | ユーザー定義型 |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| non-composite/nɒn ˈkɒmpəzɪt/ | 非複合型 |
| composite/ˈkɒmpəzɪt/ | 複合型 |
| named constants | Values given names that do not change while a program runs. |
| integers/ˈɪntɪdʒəz/ | A whole number that may be positive, negative or zero. |
| pointer/ˈpɔɪntə/ | ポインタ |
| memory address/ˈmeməri əˈdres/ | A number that identifies a location in memory. |
| dereference/ˌdiːˈrefrəns/ | デレファレンス |
| dynamic structures | Data structures that can grow or shrink while a program runs. |
| linked list/lɪŋkt lɪst/ | A sequence of nodes connected by references or pointers; the nodes need not occupy adjacent memory locations. |
| nodes/nəʊdz/ | Individual parts of a data structure that hold data and links to other parts. |
| tree/triː/ | A structure of connected nodes that can split into branches. |
| record/ˈrekɔːd/ | レコード |
| procedure/prəˈsiːdʒə/ | A named sequence of instructions that performs a task when called. |
| set/set/ | セット |
| membership test/ˈmembəʃɪp test/ | A check of whether a particular value is present in a set. |
| union/ˈjuːnɪən/ | The set of elements that belong to at least one of the sets being combined. |
| intersection/ˌɪntəˈsekʃn/ | The set of elements that belong to all of the sets being compared. |
| class/klæs/ | クラス |
| instances/ˈɪnstənsɪz/ | A particular occurrence or example; in object-oriented programming, an object created from a class. |
| objects/ˈɒbdʒekts/ | An entity in a program with associated state and behaviour, often an instance of a class. |
| data fields | Named places in a record or object that hold its data values. |
| attributes/ˈætrɪbjuːts/ | 属性 |
| methods/ˈmeθədz/ | メソッド |
| identifier/aɪˈdentɪfaɪə/ | A name used to identify a program element such as a variable, function or class. |
| indirection | Access to data through an address held by a pointer. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
ミスを不可能にする型
- 病院システムが患者の血液型を
STRINGとして保存していました。「O+」の代わりに「0+」(文字の代わりゼロ)を入力した人がいました。フィールドはそれを受理しました。なぜなら、文字列はどんな値でも受け入れるからです。 - プログラム内に「0+」が血液型ではないことを判別する手段はなく、このエラーは影響を与えるポイントまで修正されずに伝播しました。
- フィールドに8つの血液型に厳密に一致する値を持つ型を与えると、プログラムが実行される前にコンパイラによって書き込み的瞬间に割り当てが拒絶されます。
- これがユーザ定義型が提供するものです。このレッスンはシラバスで名称が指定されているもの、およびそれらの間の選択方法に関するものです。

ユーザ定義型が必要な理由
- 組み込み
STRINGでは、少数の合法的な値のいずれかを持つべきフィールドに無意味な値を格納できてしまいます。ユーザ定義型はこれを制限するため、コンパイラはより多くのミスを検出できます。 - 現実のエンティティは通常、異なる型の値の集合体であり、組み込み型は1つしか保持できません。
DECLARE MyTaxi : Vehicleは自己説明的です: 変数が何を表すかを明言しますが、DECLARE MyTaxi : STRINGは単にその記憶上の形式だけを説明します。
Why are user-defined types needed? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Restriction, grouping and self-documentation. Speed is not the reason; correctness and clarity are.
非複合型: 列挙型
- 非複合型は単一の値を保持します;複合型は複数の値をグループ化します。
- 列挙型は、名前付き定数の固定リストである値を持ちます。これらの名前は新しい型の値であり、内部では小さな整数として格納されます。
TYPE Vehicle = (M100, M230, T101, T102, T120, T150)
DECLARE MyTaxi : Vehicle
MyTaxi ← T102 // anything outside the list is rejected
- 用途: 曜日の名称、色、ステータスコード、血液型など、法的に許容される値が短い既知のリストで表せる分野すべて。

6つの名前付き値のみが割り当て可能であり、それ以外には割り当てられない

An enumerated type holds:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
An enumerated type restricts a variable to one of a fixed set of named values (e.g. days of the week).
非合成型: ポインタ
- ポインタは、他の変数のメモリアドレスまたは
NULL(「ターゲットなし」を意味する)を保持します。 - ポインタを**参照解除(dereference)**するには、記法
p^を用いて、それが指す変数にアクセスすることです。
TYPE PNode = ^TNode // a pointer to a TNode
DECLARE p : PNode
p ← NEW TNode
p^.Value ← 42 // dereference, then reach the field
- ポインタによって動的な構造体が可能になります。連結リストはポインタで結合されたノードであり、木構造は2つのポインタを持つノードです。また、大きなレコードをプロシージャに渡す際にもコピーせずに传递できます。

変数はデータそのものではなく、データの場所を保持しています

A pointer stores the memory address of another variable, and dereferencing it (e.g. p^) reaches the variable it points to.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Pointers (with NULL marking "points to nothing") are how dynamic structures like linked lists and trees are built.
Reaching the variable a pointer points at, written p^, is called ____.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The pointer holds an address; dereferencing follows it to the data. That is how linked lists and trees are built.
合成型
- レコードは、異なるタイプのフィールドを1つの名前の下でまとめ、
TYPE … ENDTYPEブロック内に配置します。トピック10で触れました。 - セットは、順序を持たない 一意の値の集合であり、add、remove、メンバーシップテスト、union、intersectionといった操作を持っています。
DECLARE Available : SET OF Colour
Available ← {Red, Blue}
IF Green IN Available THEN
…
ENDIF
- クラスと、そのインスタンスであるオブジェクトは、データフィールドである属性と、それに対する操作であるメソッドを統合します。

異なるタイプのフィールド、1つの識別子

Match each user-defined type to what it holds.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
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.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A set is unordered and its values are unique. An array is the ordered structure that allows repeats.
クラス
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
- クラスは定義であり、オブジェクトはそのインスタンスです。例えれば、独自の乗客定員を持つ実際のタクシーの1台のようなものです。
- レコードとの違いはメソッドにあります。レコードはデータだけですが、クラスはデータおよびそれに伴う振る舞いです。
A class combines:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A class bundles attributes and methods; an object is an instance of a class.
What distinguishes a class from a record?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Both group fields; only the class carries behaviour. An object is one instance of the class.
演習例: 型を選択し理由を示す
- 予約システムでは、各予約について、Requested、Confirmed、Cancelled のいずれかであるステータスを保持します。どのようなユーザー定義型を使用すべきか、そしてなぜか?
- 列挙型: 法的に許容される値は短い固定リストであるため、この型により他の値の割り当てを防ぎ、コードがドメインと同じように読み取れます。
- 予約自体については、参照、日付、乗客数、価格? レコード: 値が異なるタイプであり、常に1回の予約全体を説明するため。
- まだ空いている座席のセットについては? セット: 順序を持たず、重複がなく、メンバーシップチェックが必要な操作であるため。
between them の選択
| 要件 | 型 |
|---|---|
| 短い固定リストからの値 | 列挙型 |
| 間接参照、または成長する構造体 | ポインタ |
| 1つのものを記述する複数の異なるタイプのフィールド | レコード |
| 順序を持たない一意の値の集合 | セット |
| 状態およびそれに作用する振る舞い | クラス |
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:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
A fixed list of allowed values is exactly what an enumerated type is for.
見落としやすいポイント
- 列挙型の値は名前付き定数であり、文字列ではありません。
MyTaxi ← "T102"は別の概念であり、誤りです。 - ポインタはアドレスを保持しており、値そのものではありません。参照解除が値に到達するステップです。
- セットは順序を持たず重複がありません。配列は順序があり重複があります。問題文の表現からどちら应选择がわかります。
- クラスをレコードより優れているとさせるのはメソッドです。違いを問われたらそう答えてください。
理解できましたか
- ユーザー定義型は、格納可能なものを制限し、関連する値をグループ化し、コードを自己説明的にします。
- 非合成型: 列挙型(名前付き定数の固定リスト)、ポインタ(アドレス、参照解除によってアクセス)
- 合成型: レコード(異なるタイプのフィールド)、セット(順序なし、一意)、クラス/オブジェクト(属性+メソッド)
- 要件から選択し、一致する特性(固定リスト、間接参照、混合フィールド、一意性、状態と振る舞い)に基づいて正当化してください。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。