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/ | เมท hod |
| 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. |
ประเภทที่ทำให้ความผิดพลาดเป็นไปไม่ได้
- ระบบโรงพยาบาลจัดเก็บหมู่เลือดผู้ป่วยเป็น
STRINGHoodie คนหนึ่งพิมพ์ "0+" แทน "O+": ตัวเลขศูนย์แทนตัวอักษร Field นี้ยอมรับมัน เพราะ string ยอมรับทุกอย่าง - ไม่มีอะไรในโปรแกรมบอกได้ว่า "0+" ไม่ใช่หมู่เลือด ดังนั้นความผิดพลาดจึงเดินทางโดยไม่ถูกตรวจจับไปยังจุดที่สำคัญ
- กำหนดชนิดของฟิลด์ให้มีค่าเป็นหมู่เลือดทั้งแปดอย่างพอดี และการกำหนดค่าจะถูกปฏิเสธทันทีที่เขียนโดยคอมไพเลอร์ ก่อนที่โปรแกรมจะถูกเปิดใช้งาน
- นี่คือสิ่งที่ ประเภทที่กำหนดโดยผู้ใช้ (user-defined types) มอบให้คุณ บทเรียนนี้คือประเภทที่หลักสูตรกำหนด และวิธีการเลือกระหว่างประเภทเหล่านั้น

ทำไมถึงต้องใช้ user-defined types
STRINGที่สร้างมาในเครื่อง (built-in) ทำให้คุณสามารถเก็บค่าที่ไม่ถูกต้องลงในฟิลด์ที่ควรจะมีเพียงค่าที่ถูกต้องเท่านั้นได้; แต่ประเภทที่กำหนดโดยผู้ใช้จะ จำกัด ค่าเหล่านี้ไว้ ทำให้คอมไพลอร์ตรวจจับความผิดพลาดได้มากขึ้น- Real entities มักจะเป็น collection ของค่าหลายประเภท, และ built-in type เก็บได้เพียงอย่างเดียว
DECLARE MyTaxi : Vehicleเป็น self-documenting: มันบอกว่าเป็นอะไร, ในขณะที่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.
Non-composite types: enumerated
- Non-composite type เก็บ single value; Composite type รวมหลายค่าเข้าด้วยกัน
- ประเภทที่กำหนดเอง (enumerated type) มีค่าที่เป็น รายการคงที่ที่มีชื่อกำหนด ชื่อเหล่านี้คือค่าของชนิดใหม่ ที่จัดเก็บภายในเป็นจำนวนเต็มขนาดเล็ก
TYPE Vehicle = (M100, M230, T101, T102, T120, T150)
DECLARE MyTaxi : Vehicle
MyTaxi ← T102 // anything outside the list is rejected
- การใช้: วันในสัปดาห์, สี, รหัสสถานะ, กลุ่มเลือด: ทุกสนาม whose ค่าที่ถูกต้องเป็นรายการสั้นๆ ที่ทราบอยู่แล้ว

หกลิสต์ที่มีชื่อกำหนด และไม่มีอะไรอื่นที่ระบุได้
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).
ประเภทที่ไม่รวมองค์ประกอบ: ปอยเตอร์
- ปอยเตอร์ (pointer) เก็บ ที่อยู่หน่วยความจำของตัวแปรอื่น, หรือ
NULLความหมายว่า "ไม่มีเป้าหมาย" - การ ดีรีERENCE (dereference) ปอยเตอร์, เขียนว่า
p^, คือการเข้าถึงตัวแปรที่มันชี้ไป
TYPE PNode = ^TNode // a pointer to a TNode
DECLARE p : PNode
p ← NEW TNode
p^.Value ← 42 // dereference, then reach the field
- พอยเตอร์คือสิ่งที่ทำให้โครงสร้างแบบ ไดนามิก เป็นไปได้: ลิสต์เชื่อมโยงเป็นโหนดที่เชื่อมต่อกันด้วยพอยเตอร์, และต้นไม้เป็นโหนดที่มีสองตัว มันยังส่งเรคอร์ดขนาดใหญ่ไปยังโปรซีเจอร์โดยไม่ต้องคัดลอกมัน

ตัวแปรเก็บตำแหน่งที่ข้อมูลอยู่ ไม่ใช่ข้อมูลเอง
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.
ประเภทที่รวมองค์ประกอบ
- เรคอร์ด (record) รวมฟิลด์ของ ชนิดที่แตกต่างกัน ภายใต้ชื่อเดียว, ใน
TYPE … ENDTYPEบล็อก คุณเคยพบมันในหัวข้อ 10 - เซต (set) คือกลุ่ม ไม่เรียงลำดับ ของ ค่าที่ไม่ซ้ำกัน, พร้อมการดำเนินการเพิ่ม, ลบ, ตรวจสอบสมาชิกภาพ, ยูเนียน และอินเตอร์เซกชัน
DECLARE Available : SET OF Colour
Available ← {Red, Blue}
IF Green IN Available THEN
…
ENDIF
- คลาส (class) และอินสแตนซ์ของมัน, ออบเจ็กต์ (objects), รวมฟิลด์ข้อมูล, แอททริบิวต์ (attributes) เข้ากับการดำเนินการกับมัน, เมทოდ (methods)

ฟิลด์ของชนิดต่างกัน, ตัวระบุชื่อเดียว
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
- คลัสคือ นิยาม; ออบเจ็กต์คือ อินสแตนซ์ ของมัน, TAXI จริงหนึ่งคันที่มีความจุของตัวเอง
- สิ่งที่แตกต่างจากเรคอร์ดคือ เมทอด: เรคอร์ดเป็นข้อมูลเพียงอย่างเดียว, คลัสคือข้อมูล และ พฤติกรรมที่ควรอยู่กับมัน
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 คุณควรใช้ประเภทที่กำหนดโดยผู้ใช้ชนิดใด และทำไม?
- ประเภทที่กำหนดเอง: ค่าที่ถูกต้องเป็นรายการสั้นคงที่ ดังนั้นชนิดนี้จะป้องกันไม่ให้ค่าอื่นถูกกำหนด และโค้ดอ่านได้เหมือนโดเมน
- และสำหรับการจองเอง: อ้างอิง, วันที่, จำนวนผู้โดยสาร และราคา? เรคอร์ด, เพราะค่าเป็นชนิดต่างกันและอธิบายการจองหนึ่งครั้งด้วยกันเสมอ
- และสำหรับชุดที่นั่งที่ยังว่าง? เซต: ไม่เรียงลำดับ, ไม่มีซ้ำ, และการตรวจสอบสมาชิกภาพคือการดำเนินการที่คุณต้องการ
การเลือกระหว่างทั้งสอง
| ความต้องการ | ชนิด |
|---|---|
| ค่าจากรายการสั้นคงที่ | type ที่กำหนดเอง |
| การอ้างอิงทางอ้อม, หรือโครงสร้างที่ขยายขนาดได้ | ปอยเตอร์ |
| ฟิลด์หลายชนิดที่มีประเภทต่างกันเพื่ออธิบายสิ่งเดียวกัน | เรคอร์ด (record) |
| กลุ่มไม่เรียงลำดับของค่าที่ไม่ซ้ำกัน | เซต |
| สถานะ และ พฤติกรรมที่ทำต่อมัน | คลาส |
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.
คะแนนที่หลุดหายไป
- ค่าของ type ที่กำหนดเองคือ ค่าคงที่มีชื่อ, ไม่ใช่สตริง.
MyTaxi ← "T102"เป็นอีกอย่างหนึ่งและไม่ถูกต้อง - ปอยเตอร์เก็บ ที่อยู่, ไม่ใช่ค่า. การดีรีERENCEคือขั้นตอนที่เข้าถึงค่า
- เซต ไม่เรียงลำดับและไม่มีซ้ำ; แอร์เรย์ เรียงลำดับและอาจซ้ำ. คำใช้คำถามบอกคุณว่าอันไหน
- สิ่งที่ทำให้คลัสมากกว่าเรคอร์ดคือ เมทอด. บอกเมื่อถูกถามถึงความแตกต่าง
คุณเข้าใจแล้ว
- user-defined types จำกัดสิ่งที่สามารถจัดเก็บ, รวมค่าที่เกี่ยวข้องด้วยกัน, และทำให้โค้ดอธิบายตัวเองได้
- non-composite: type ที่กำหนดเอง (รายการคงที่มีชื่อกำหนด) และ ปอยเตอร์ (ที่อยู่, เข้าถึงได้โดยการ ดีรีERENCE)
- คอมโพสิต: เรคอร์ด (fields ที่มีประเภทต่างกัน), เซต (ไม่เรียงลำดับ, มีค่าซ้ำไม่ได้), คลาส/ออบเจกต์ (แอททริบิวต์ บวกกับ เมธอด)
- เลือกจากความจำเป็น, และอธิบายด้วยคุณสมบัติที่ตรง: รายการคงที่, การอ้างอิงทางอ้อม, ฟิลด์ผสม, ความไม่ซ้ำ, หรือสถานะพร้อมพฤติกรรม
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้