Classes & objects · כיתות ואובייקטים
A class is a blueprint
- A class defines a new kind of thing — a user-defined type.
- An object is one item made from that class.
- One
Dogclass can make many dog objects, each with its own data.
מחלקה היא תבנית
- מחלקה מגדירה סוג חדש של דבר — סוג מוגדר על ידי המשתמש.
- אובייקט הוא פריט אחד המיוצר ממחלקה זו.
- מחלקה אחת
Dogיכולה ליצור מספר אובייקטי כלבים, כל אחד עם נתונים משלו.
init and attributes
__init__is a special method that runs when you make an object.selfrefers to the object being made.- Values stored on
selfare the object's attributes (its data).
init ותכונות
__init__היא שיטה מיוחדת שרצה כשאתה יוצר אובייקט.selfמתייחס לאובייקט שנמצא ביצירה.- ערכים מאוחזים ב
selfהם ה-תכונות של האובייקט (הנתונים שלו).
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(2, 5)
print(p.x)
print(p.y)
Methods
- A method is a function defined inside a class.
- Its first parameter is always
self, the object it works on. - Call it with a dot:
object.method().
שיטות
- שיטה היא פונקציה המוגדרת בתוך מחלקה.
- הפרמטר הראשון שלה הוא תמיד
self, האובייקט עליו היא פועלת. - קרא אותה באמצעות נקודה:
object.method().
class Greeter:
def __init__(self, name):
self.name = name
def hello(self):
return "Hi, " + self.name
g = Greeter("Sam")
print(g.hello())
Enumerated types
- An enumerated type is a fixed set of named values.
from enum import Enumlets you define one as a class.- It is clearer than using loose numbers like
1,2,3.
סוגים ממוספרים
- סוג ממוספר הוא קבוצה קבועה של ערכים מוגדרים.
from enum import Enumמאפשר להגדיר אותו כמחלקה.- זה ברור יותר מאשר שימוש במספרים רחבים כמו
1,2,3.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.GREEN)
print(Color.GREEN.value)
In Cambridge pseudocode
- A
CLASSblock lists its data and aNEWconstructor.
בפסאודוקוד קמברידג'
- בלוק
CLASSמפרט את הנתונים שלו וכולל בונהNEW.
CLASS Point
PRIVATE X : INTEGER
PRIVATE Y : INTEGER
PUBLIC PROCEDURE NEW(startX : INTEGER, startY : INTEGER)
X ← startX
Y ← startY
ENDPROCEDURE
ENDCLASS
DECLARE p : Point
p ← NEW Point(2, 5)
Common mistakes
- Every method takes
selfas its first parameter. __init__runs automatically when you create an object.
טעויות נפוצות
- לכל שיטה יש ל
selfכפרמטר ראשון. __init__רץ אוטומטית כשאתה יוצר אובייקט.
Now you try
- Define each class with
__init__and any methods it needs. - Press Check answer to test your code.
כעת תנסו בעצמכם
- הגדר כל מחלקה עם
__init__וכל שיטות שהיא זקוקה להם. - לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Define a class Dog whose __init__ takes name and age and stores them as attributes self.name and self.age. · הגדר כיתה Dog שבה ה-__init__ מקבל name ו-age ומאחסן אותם כמאפיינים (attributes) self.name ו-self.age.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Define a class Rectangle with __init__(self, width, height) and a method area(self) that returns width * height. · הגדר כיתה Rectangle עם ⟨__init__(self, width, height)⟩ ושיטה area(self) שחזרה width * height.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Define a class BankAccount. __init__ sets self.balance to 0. Add deposit(self, amount) to add money and withdraw(self, amount) to take it away. · הגדר כיתה BankAccount. ה-__init__ מגדיר את self.balance ל-0. הוסף ⟨deposit(self, amount)⟩ להוסיף כסף ו-withdraw(self, amount) להוציא אותו.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.