การประกอบและการสืบทอด
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| inheritance/ɪnˈherɪtəns/ | การสืบทอด |
| composition/ˌkɒmpəˈzɪʃn/ | การจัดองค์ประกอบ |
| fragile base class/ˈfrædʒaɪl beɪs klæs/ | คลาสฐานที่เปราะบาง |
One fix, a thousand wrong receipts
- A shop adds a loyalty bonus to its receipt total. The next day every discount receipt is wrong, though nobody touched the discount code.
- The discount class had copied the old total code instead of reusing it. How classes connect decides whether a fix in one place stays a fix everywhere.
Two ways to connect classes
- Inheritance 继承 says is-a: a
Memberis aPerson. Composition 组合 says has-a: aLibraryhas aCatalog. - Inheritance ties classes tightly. When a change to a base class breaks its children, that is the fragile base class 脆弱基类 problem.
super() builds the base first
super().__init__(...)runs the base class's set-up first, soPerson.__init__finishes before anyMemberline runs. Skip it andself._namedoes not exist whenMemberneeds it.
class Member(Person):
def __init__(self, name, card):
super().__init__(name)
self._card = card
self._loans = 0
Member.init เริ่มต้นด้วย super().init(name) เพื่อให้ —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ลำดับคือฐานก่อน: ข้าม super() และ self._name ยังไม่มีอยู่เมื่อ Member ต้องการใช้
เรียงลำดับขั้นตอนของ Member('Ana', 7) ให้ถูกต้อง
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
คลาสแม่จะถูกสร้างก่อนเสมอ ดังนั้น Member จึงสามารถพึ่งพา _name ได้
Override by delegating
- To change a base method, call it and adjust the result:
return super().total() - 10. The base's loyalty bonus then stays in your total. - Copying the base's code into the override makes a fork. The day the base gains a line, the copy goes stale: that was the receipt bug.
class Receipt:
def total(self):
# the new loyalty line
t = sum(self.prices)
return t - self.bonus()
# Bug: copied, so no bonus
class DiscountReceipt(Receipt):
def total(self):
return sum(self.prices) - 10
# Fix: delegate to the base
class DiscountReceipt(Receipt):
def total(self):
return super().total() - 10
การ Override ที่คัดลอกโค้ดจากวิธีเดิม —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
โค้ดที่ถูกคัดลอกเป็นทางแยก: คลาสแม่ยังคงเปลี่ยนแปลงในขณะที่ส่วนที่คัดลอกนิ่งอยู่
Receipt.total() คืนค่า 100 จากนั้นคลาสแม่เพิ่มบรรทัดความภักดีที่ทำให้มูลค่าเพิ่มขึ้น 5 DiscountReceipt.total() เขียนเป็น super().total() − 10 ตอนนี้มันจะคืนค่าอะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
คลาสแม่ตอนนี้ให้ค่า 95 และการ Override จะหักลบ 10 ออกจากค่านั้น หากใช้การคัดลอกโค้ดจะยังแสดงค่า 90 อยู่
Composition delegates
- A
Libraryholds itsCopyobjects and passes questions on:available()asks each partnot c.is_out()and counts the answers. - The library holds no borrowing logic of its own. Each part keeps its own rules.

Library และ Catalog เชื่อมต่อกันได้ดีที่สุดโดย —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Has-a คือ composition: ส่วนรวมเป็นเจ้าของส่วนย่อยและส่งต่อคำขอ; เปลี่ยนส่วนย่อยแต่คงอินเทอร์เฟซไว้
Swap the part, keep the whole
- Replace the list of copies with a database-backed collection and
Librarydoes not change: parts change, the whole keeps its interface. - Rule of thumb: inheritance for a true is-a, composition for everything else.
คลาส Library ควรสืบทอดจาก list เพื่อให้สามารถเก็บสำเนาได้
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Library มีสำเนาอยู่แต่ไม่ใช่ list การประกอบ (Composition) ช่วยทำให้ list เปลี่ยนแทนได้และซ่อนวิธีทำงานของ list ที่ Library ไม่ควรเปิดให้ใช้งาน
Worked example: building and counting
Member('Ana', 7)startsMember.__init__, which callsPerson.__init__('Ana')to set_name; then_card = 7and_loans = 0. Twoborrow()calls make_loans2.- A library gets 5 copies and 2 are issued:
available()asks all 5 and returns 3.
Library มี 8 สำเนา 3 ถูกยืมออกไป แล้ว 1 ของนั้นถูกนำกลับมา What does available() return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
มี 2 สำเนายังคงถูกยืมอยู่ ดังนั้น 8 − 2 = 6 จึงเป็นจำนวนที่พร้อมให้ยืม
Three common slips
- Inheriting just to reuse two methods. Use composition or a plain function.
- Forgetting
super().__init__. The base's attributes then never exist. - Copying base code into an override. The fork goes stale; delegate with
super().
Recap
| Inheritance | Composition | |
|---|---|---|
| Says | is-a: a Member is a Person |
has-a: a Library has a Catalog |
| Ties classes | tightly | loosely |
| Rule | call super() so the base is built first |
delegate questions to the parts |
- Override by calling the base, never by copying it.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้