الكائنات والفئات
مُقدَّم
5:16
البرمجة كائنية التوجه
This course is about how a small script grows into a system that other people can safely change. The first two units build the object: classes, references and…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| object-oriented programming/ˈɒbdʒekt ˈɔːrɪəntɪd ˈprəʊɡræmɪŋ/ | البرمجة كائنية التوجه |
| object/ˈɒbdʒekt/ | الكائن |
| class/klæs/ | الفئة |
| instance/ˈɪnstəns/ | الحالة |
| instance attribute/ˈɪnstəns ˈætrɪbjuːt/ | صفة الحالة |
| class attribute/klæs ˈætrɪbjuːt/ | صفة الفئة |
| reference/ˈrefrəns/ | الإشارة |
| alias/ˈeɪlɪəs/ | التسمية المستعارة |
| equality/iːˈkwɒlɪti/ | التساوي |
| identity/aɪˈdentɪti/ | الهوية |
One home for each book
- A library app tracks thousands of books. If titles and copy counts sit in loose variables, one slip updates the wrong book and nobody can tell where.
- Object-oriented programming 面向对象程序设计 gives each book one home: an object that owns its own data and actions.
Classes and instances
- A class 类 is a blueprint. An object 对象 is one thing built from it, also called an instance 实例 of the class.
Bookis the class; the copy of Signals on the shelf is one instance, and Maps is another.
The constructor and self
__init__runs when an object is created and sets up its instance attributes 实例属性. Every method takesself, which is the instance itself.
class Book:
next_id = 1 # shared
def __init__(self, title, n):
self.id = Book.next_id
Book.next_id += 1
self.title = title
self.copies = n
def take(self):
self.copies -= 1
return self.copies
خصائص العينة تأتي من —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
تعمل init أثناء البناء وتزود كل عينة بقيم خصائصها الخاصة.
Class attributes are shared
- Class attributes 类属性 live on the class itself, and every instance reads the same one.
Book.next_idbelongs there so every caller agrees on the next number. - Instance attributes such as
titleandcopiesare separate for each object.
يتم نسخ سمة الفئة إلى كل كائن عند إنشاء الكائن.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
تبقى على الفئة، وتقرأ كل كائن تلك القيمة المشتركة الواحدة.
Names are references
- Assignment never copies an object. A variable holds a reference 引用 to it, so
c = amakes two aliases 别名 for one object. - A change made through either name is visible through both.

بعد تنفيذ c = a، أي عبارة صحيحة؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
عملية الإسناد تنسخ المرجع وليس الكائن: التغيرات عبر أي من الأسماء البديلة تظهر عبر كليهما.
x = Book('Maps', 5)، ثم y = x، ثم يتم استدعاء y.take() ثلاث مرات. ما هي قيمة x.copies؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
x و y يمثلان كائناً واحداً، لذا فإن ثلاث استدعاءات لـ take من 5 تترك 2.
Equality and identity
- Equality 相等 (
==) compares values, once a class defines how. Identity 同一 (is) asks whether two names point to the very same object. - If a class does not define
==, Python falls back to identity, so two separate books with the same title are not equal.
صل كل مصطلح بمعناه.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
هذه الكلمات الأربع تصف كل برنامج يعتمد على البرمجة كائنية التوجه.
Where Python looks for an attribute
obj.xis looked up from the inside out: the instance first, then the class, then the base classes.- So
a.next_id = 9creates an instance attribute 实例属性 that hides the class value foraonly.Book.next_idis untouched.
رتب الأماكن التي يبحث عنها بايثون لإيجاد obj.x.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
المطابقة الأولى هي التي تنتصر، ولهذا السبب يمكن لخاصية العنصر إخفاء خاصية الفئة.
Worked example: trace the state
- Run this, then ask what each name sees:
a = Book('Signals', 3)
b = Book('Signals', 3)
c = a
a.take()
a.take()
a.copiesis 1,b.copiesis 3, andc.copiesis 1, becausecisa: one object 对象 with two names.a == bis False,a is cis True, andBook.next_idreads 3: ids 1 and 2 are used.
يوجد instanceان من Book؛ يقرأ Book.next_id —
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
بدأت خاصية الفئة عند 1 وزادت مرة واحدة في كل بناء، لذا فهي الآن تقرأ 3.
بعد التتبع، يقوم شخص ما بتشغيل a.next_id = 9 ثم d = Book('Maps', 1). ما هي قيمة d.id؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
a.next_id = 9 يخفي القيمة فقط بالنسبة إلى a. كانت Book.next_id لا تزال 3، لذا تحصل d على id 3.
Three common slips
- Forgetting
selfin a method header or before an attribute name. - Setting a class attribute through an instance. It only hides the shared value for that one object 对象.
- Expecting
d = cto copy. It makes an alias.
Recap
- Class = blueprint, instance = one object;
__init__sets instance attributes 实例属性 throughself. - Class attributes are shared; lookup goes instance, then class, then bases.
- Assignment makes aliases. With
c = aandba separate copy of the same book:
| Test | Asks | a, c |
a, b |
|---|---|---|---|
== |
equal under the class’s comparison? | True |
False |
is |
same object? | True |
False |
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.