Object-oriented programming
แนะนำใหม่
13:52
แนวทางการเขียนโปรแกรม (Programming Paradigms)
นี่คืองานหนึ่ง: รายการนักเรียนทุกคนที่ได้เกรด A นักเขียนโปรแกรมคนหนึ่งเขียนลูป — เริ่มต้นที่บันทึกแรก ตรวจสอบเกรด เพิ่มชื่อลงในรายการ เลื่อนไป…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| public/ˈpʌblɪk/ | Describes a class member that code outside the class can access. |
| routines/ruːˈtiːnz/ | Sections of code written to perform particular tasks within a program. |
| object/ˈɒbdʒekt/ | วัตถุ |
| private/ˈpraɪvət/ | Describes a class member that code outside the class cannot access directly. |
| method/ˈmeθəd/ | A defined way of performing a task or investigation; in programming, a function associated with a class or object. |
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | การห่อหุ้ม |
| object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ | A way of designing programs using objects that combine data and methods. |
| class/klæs/ | คลาส |
| attributes/ˈætrɪbjuːts/ | A named property or characteristic associated with an object, element or record. |
| state/steɪt/ | The saved information needed to continue a program from its current point. |
| behaviour/bɪˈheɪvjə/ | The actions an object can perform through its methods. |
| instance/ˈɪnstəns/ | อินสแตนซ์ |
| visibility/ˌvɪzɪˈbɪlɪti/ | The rules that control where a class member can be accessed. |
| constructor/kənˈstrʌktə/ | ตัวสร้าง |
| getters | Methods that allow other code to read an object's attribute values. |
| setters | Methods that allow other code to change an object's attribute values. |
| get methods | Methods that allow other code to read an object's attribute values. |
| set methods | Methods that allow other code to change an object's attribute values. |
| Inheritance/ɪnˈherɪtəns/ | An is-a relationship. |
| subclass/ˈsʌbklæs/ | คลาสลูก |
| superclass/ˈsuːpəklæs/ | คลาสแม่ |
| overriding/ˌəʊvəˈraɪdɪŋ/ | Providing a subclass implementation of an inherited instance method with a matching signature, subject to the language's rules. |
| Polymorphism/ˈpɒlɪmɔːfɪzəm/ | The same call behaving differently per class, which is what lets a new subclass be added without changing existing code. |
| Abstraction/əbˈstrækʃn/ | Representing essential features while leaving out unnecessary detail. |
| method call | An instruction that asks an object to perform one of its methods. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| list/lɪst/ | A collection of items arranged in a particular order. |
| interface/ˈɪntəfeɪs/ | A way for users or programs to communicate with the computer's hardware. |
| is-a relationship | |
| Containment/kənˈteɪnmənt/ | An object holds another object as one of its attributes. |
| aggregation/ˌæɡrɪˈɡeɪʃn/ | การรวมกลุ่ม |
| references/ˈrefrənsɪz/ | Values that identify objects so that code can access them. |
| collection/kəˈlekʃn/ | A group of items stored together so they can be managed and used. |
| string/strɪŋ/ | A sequence of characters treated as a single data value. |
| composition/ˌkɒmpəˈzɪʃn/ | A relationship where an object contains parts that depend on it to exist. |
| members | The attributes and methods that belong to a class or object. |
การเปลี่ยนแปลงที่จุดเดียวที่เคยหมายถึงการเปลี่ยนแปลงห้าสิบจุด
- โปรแกรมเก็บยอดเงินลูกค้าเป็นตัวเลขสาธารณะ มี twenty routines อ่านและ fifteen เขียน ตอนนี้ยอดเงินไม่ควรติดลบเลย
- Without objects, that rule has to be added at fifteen places, and the day someone adds a sixteenth writer it is broken again.
- With objects, the balance is private and the only way to change it is a method that enforces the rule. There is one place to add it and no way round it.
- นั่นคือ การห่อหุ้ม (encapsulation) และเป็นเหตุผลว่าทำไมการเขียนโปรแกรมเชิงวัตถุจึงมีอยู่ บทเรียนนี้เกี่ยวกับวัตถุ คลาส และสี่เสาหลัก

วัตถุและคลาส
- วัตถุ รวม คุณสมบัติ (attributes) ซึ่งเป็นข้อมูลของมัน เข้ากับ เมทოდ (methods) ซึ่งเป็นการทำงานที่ทำต่อข้อมูลนั้น มันคือหน่วยของสถานะและพฤติกรรมรวมกัน
- คลาส คือแบบร่าง: มันกำหนดว่าทุกวัตถุชนิดนี้จะต้องมีคุณสมบัติและเมทอดอะไรบ้าง วัตถุ เป็น อินสแตนซ์ (instance) ของคลาส โดยมีค่าของตัวเอง
- คลาสหนึ่ง
Taxiและแท็กซี่จริงแต่ละคันเป็นวัตถุที่มีเลขทะเบียนและความจุของตัวเอง

คุณสมบัติอยู่ด้านบน เมทოდอยู่ด้านล่าง และมีเครื่องหมายแสดงระดับการเข้าถึง
Programming concept lab
Connect examples to the programming idea they show.
An object combines:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
An object bundles its data (attributes) with the operations (methods) that act on it.
What is the relationship between a class and an object?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
One Taxi class defines the attributes and methods; each actual taxi is an object with its own capacity and registration.
ตัวสร้าง, getter และ setter
- ตัวสร้าง (constructor) เป็นเมทอดพิเศษที่ทำงาน เมื่อมีการสร้างวัตถุ เพื่อตั้งค่าคุณสมบัติให้เป็นค่าเริ่มต้นที่ถูกต้อง
- เนื่องจากคุณสมบัติเป็นส่วนตัว คลาสจึงมี get methods เพื่ออ่านค่า และมี set methods เพื่อเปลี่ยนแปลง ค่า set method คือจุดที่กฎเช่น "ห้ามติดลบ" ถูกบังคับใช้
- นั่นคือสิ่งที่ทำให้คุณสมบัติส่วนตัวมีค่า: กฎถูกบังคับใน ทุก การเปลี่ยนแปลง เพราะไม่มีเส้นทางอื่นไปยังข้อมูล
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC PROCEDURE NEW(StartCapacity : INTEGER)
Capacity ← StartCapacity
ENDPROCEDURE
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS

One class, separate object state
Trace which instance receives each call. Compare accepted and rejected operations while keeping both balances nonnegative.
A constructor is a special method that:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The constructor initialises a new object's attributes at creation time.
เสาหลักที่ 1: การห่อหุ้ม
- การห่อหุ้ม หมายถึงข้อมูลของวัตถุถูก ซ่อนไว้เบื้องหลังเมทอด คุณสมบัติเป็นส่วนตัว; โค้ดภายนอกใช้เมทოდสาธารณะ
- ประโยชน์สองประการ และข้อสอบต้องการทั้งสองอย่าง: วัตถุสามารถ บังคับใช้กฎของตัวเอง ในทุกการเปลี่ยนแปลง และการแสดงออกภายในสามารถ ถูกเปลี่ยนแปลงได้อย่างปลอดภัย ในภายหลัง เนื่องจากไม่มีโค้ดภายนอกขึ้นอยู่กับมัน
- เก็บความสมดุลtoday ในปอนด์และพรุ่งนี้ในเพนนี: ผู้ใช้งานทุกคนของคลาสยังคงเรียก
GetBalance()และไม่มีอะไรภายนอกเสียหาย
Encapsulation means:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Encapsulation hides the data; outside code uses the public methods, so internals can change safely.
What does encapsulation buy? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Encapsulation is about maintainability and correctness, not speed. Private data with public methods is what makes both possible.
เสาหลักที่ 2: การสืบทอด
- การสืบทอด ทำให้ subclass สามารถกำหนดโดยอ้างอิงจาก superclass รับคุณสมบัติและmethods ทั้งหมดของมันมา แล้วเพิ่มหรือ override ของมันเอง
- มันจำลองความสัมพันธ์ "เป็น-ของ" (is-a): ผู้จัดการ เป็น พนักงาน ดังนั้น
ManagerสืบทอดมาจากEmployeeและเพิ่มแผนก - ประโยชน์คือโค้ดที่แชร์กันถูกเขียน ครั้งเดียว ในsuperclass และการเปลี่ยนแปลงมันจะส่งถึงทุกsubclass

ส่วนที่ร่วมกันอยู่ด้านบน; ส่วนต่าง ๆ อยู่ด้านล่าง
Inheritance models an "is-a" relationship (a Manager is an Employee), so a subclass inherits and extends its superclass.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Inheritance is "is-a" specialisation; "has-a" relationships are modelled by composition instead.
Inheritance models an "____ a" relationship, such as a Manager being an Employee.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A car has an engine, which is composition rather than inheritance. Getting that distinction right is what the question tests.
เสาหลักที่ 3 และ 4: พอลิโมर्फिซึมและนามธรรม
- Polymorphism หมายถึง ออบเจ็กต์ต่างๆ ตอบสนองต่อ การเรียกmethod เดียวกัน ในรูปแบบของตัวเอง ทุก
ShapeมีArea()method;CircleและRectangleนำไปใช้ต่างกัน - ค่าของมัน: โค้ดสามารถวนลูปผ่านรายการของรูปร่างโดยเรียก
Area()กับแต่ละตัวโดยไม่จำเป็นต้องรู้หรือสนใจว่าแต่ละตัวเป็นประเภทใด เพิ่มคลาสTriangleในภายหลังและวงลูปนั้นไม่ต้องการการเปลี่ยนแปลงเลย - นามธรรม (Abstraction) หมายถึงการแสดงอินเทอร์เฟซที่เรียบง่ายและซ่อนความซับซ้อนไว้เบื้องหลัง: ผู้เรียก
Area()ไม่จำเป็นต้องรู้เรื่องเรขาคณิตเลย

Polymorphism allows:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The same call (e.g. Area()) behaves correctly for each type — the caller need not know the exact class.
Put the consequences of polymorphism in order, from the definition to the benefit.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
One call, several implementations, and the code that uses it never has to know about the new ones.
ตัวอย่างทำโจทย์: ชื่อกองหลัก
- A
BankAccountclass รักษาความสมดุลเป็นส่วนตัวและมีDeposit()และWithdraw()methods ที่ปฏิเสธไม่ให้มันติดลบ. Encapsulation: ข้อมูลถูกซ่อนและกฎถูกบังคับผ่านช่องทางเดียวที่จะเข้าถึงได้ - A
SavingsAccountclass ถูกนิยามว่าเป็นBankAccountที่เพิ่มอัตราดอกเบี้ยเข้าไป การสืบทอด (Inheritance): ความสัมพันธ์ is-a นำโค้ดจาก super class มาใช้ซ้ำ - วงลูปเรียก
Print()กับเอกสารทุกชิ้นในรายการ และแต่ละประเภทพิมพ์ตัวเองแตกต่างกัน พอลิโมर्फิซึม: การเรียกเดียว, การนำไปใช้หลายรูปแบบ - ระบุชื่อเสาหลัก และ คุณสมบัติของสถานการณ์ที่แสดงออกถึงมัน

Match each OOP pillar to its meaning.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Encapsulation = hiding; inheritance = is-a specialisation; polymorphism = same interface, varied behaviour.
ตัวอย่างทำโจทย์: ทำไม OOP สำหรับระบบขนาดใหญ่
- ยกข้อดีสองประการของการใช้การเขียนโปรแกรมเชิงวัตถุสำหรับระบบขนาดใหญ่ [4]
- การห่อหุ้มหมายความว่ารายละเอียดภายในของแต่ละคลาสสามารถเปลี่ยนแปลงได้โดยไม่ส่งผลกระทบต่อส่วนอื่นๆ ของโปรแกรม ทำให้ระบบบำรุงรักษาง่ายขึ้นและทีมสามารถทำงานบนคลาสได้พร้อมกัน
- การสืบทอดหมายความว่าพฤติกรรมที่ร่วมกันถูกเขียนครั้งเดียวและใช้ซ้ำ ทำให้มีโค้ดซ้ำน้อยลงและการแก้ไขจะครอบคลุมทุกที่ในคราวเดียว
- พอลิโมर्फิซึมหมายถึงสามารถเพิ่มประเภทใหม่ได้โดยไม่ต้องเปลี่ยนแปลงโค้ดที่ใช้มัน คำตอบแต่ละข้อคือคุณสมบัติ และ ผลกระทบ
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
A team has players; it is not a player
- Containment 包含 means an object holds another object as one of its attributes. In aggregation 聚合, the contained object can exist independently: a
Teamholds references 引用 toPlayerobjects that can move to another team. - Choose by the relationship: a
Goalkeeperis aPlayer, so use inheritance; aTeamhas players, so use aggregation. A team inheriting a player'sKick()method is a design error, even if the team would enjoy taking a penalty.

A Team stores references to Player objects. Players remain when the team is removed. Which relationship fits?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The team contains independent Player objects. Inheritance would require an is-a relationship.
ตัวอย่างแบบฝึกหัด: ออกแบบทีมโดยไม่คัดลอกผู้เล่น
- ที่ทราบ: a
Playerมีชื่อแบบส่วนตัวและวิธีGetName()ออกแบบTeamโดยมีคอลเลกชันแบบส่วนตัวMembersที่มีอ้างอิงไปยังวัตถุPlayerและมีวิธีAddPlayer(NewPlayer)เพื่อเพิ่มอ้างอิง Playerสามารถมีอยู่ก่อนทีม และทีมสามารถถูกลบออกในขณะที่ผู้เล่นยังคงอยู่ การเปลี่ยนชื่อผู้เล่นผ่านเมทอดของผู้เล่นเองจะมองเห็นได้ในทุกออบเจ็กต์ที่ถือผู้เล่นนั้น; การคัดลอกสายอักขระชื่อจะเป็นข้อมูลล้าสมัย
Player object: name = "Sam"
Team object: Members = [reference to Sam's Player object]
Goalkeeper class: inherits the Player attributes and methods
In this design, removing a Team must also destroy every Player it contains.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The Players exist independently. Removing the team removes its references, not necessarily the Player objects.
Match each design to its relationship.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Inheritance models is-a; aggregation models has-a with independent objects; encapsulation controls access to data.
คะแนนที่หลุดหายไป
- คลาสคือแบบร่าง, วัตถุคืออินสแตนซ์ ของมัน อย่าใช้คำเหล่านี้แทนกันได้
- Encapsulation คือการซ่อนข้อมูลไว้หลังmethods และประโยชน์ของมันคือ การบังคับใช้กฎ และ การเปลี่ยนแปลงภายในอย่างปลอดภัย "มีความปลอดภัยมากขึ้น" เป็นคำที่กว้างเกินไป
- การสืบทอดคือความสัมพันธ์ is-a รถ มีเครื่องยนต์ ซึ่งเป็นการประกอบ (composition) ไม่ใช่การสืบทอด
- พอลิโมर्फิซึมคือการเรียกเดียวกันที่แสดงพฤติกรรมต่างกันตามคลาส ซึ่งเป็นสิ่งที่ทำให้สามารถเพิ่มซับคลาสใหม่ได้โดยไม่ต้องเปลี่ยนแปลงโค้ดเดิม
คุณเข้าใจแล้ว
- an object รวม attributes และ methods; a class เป็นแบบร่างและ object เป็น instance ตั้งขึ้นโดย constructor
- encapsulation: private data behind public methods, so rules are enforced on every change and internals can change safely
- การสืบทอด: ซับคลาสรับสมาชิกของซูเปอร์คลาสและเพิ่มหรือแทนที่มัน, จำลอง is-aและเขียนโค้ดที่ร่วมกันครั้งเดียว
- พอลิโมर्फิซึม: การเรียกเดียวกันที่นำเสนอต่างกันตามคลาส, เพื่อให้ประเภทใหม่ไม่ต้องเปลี่ยนแปลงโค้ดเดิม; นามธรรม: อินเทอร์เฟซที่เรียบง่ายที่ซ่อนความซับซ้อน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้