Classes & objects · Lớp & đối tượng
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.
Lớp là bản thiết kế
- Một lớp xác định một loại vật thể mới — một kiểu do người dùng định nghĩa.
- Một đối tượng là một thực thể được tạo ra từ lớp đó.
- Một
Doglớp có thể tạo ra nhiều đối tượng chó, mỗi đối tượng có dữ liệu riêng.
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 và thuộc tính
__init__là một phương thức đặc biệt chạy khi bạn tạo một đối tượng.selftham chiếu đến đối tượng đang được tạo.- Các giá trị được lưu trữ trên
selflà các thuộc tính của đối tượng (dữ liệu của nó).
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().
Phương thức
- Một phương thức là một hàm được định nghĩa bên trong một lớp.
- Tham số đầu tiên của nó luôn là
self, đối tượng mà nó hoạt động trên đó. - Gọi nó bằng dấu chấm:
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.
Kiểu liệt kê
- Một kiểu liệt kê là một tập hợp cố định các giá trị có tên.
from enum import Enumcho phép bạn định nghĩa một kiểu như một lớp.- Nó rõ ràng hơn so với việc sử dụng các số tùy ý như
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.
Trong pseudocode Cambridge
- Một khối
CLASSliệt kê dữ liệu của nó và một constructorNEW.
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.
Lỗi thường gặp
- Mọi phương thức đều nhận
selflà tham số đầu tiên. __init__chạy tự động khi bạn tạo một đối tượng.
Now you try
- Define each class with
__init__and any methods it needs. - Press Check answer to test your code.
Bây giờ bạn thử
- Định nghĩa mỗi lớp với
__init__và bất kỳ phương thức nào mà nó cần. - Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Define a class Dog whose __init__ takes name and age and stores them as attributes self.name and self.age. · Định nghĩa lớp Dog whose __init__ nhận name và age và lưu chúng như thuộc tính self.name và self.age.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Define a class Rectangle with __init__(self, width, height) and a method area(self) that returns width * height. · Định nghĩa class Rectangle với __init__(self, width, height) và phương thức area(self) trả về width * height.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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. · Định nghĩa lớp BankAccount. __init__ đặt self.balance bằng 0. Thêm deposit(self, amount) để thêm tiền và withdraw(self, amount) để rút tiền.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.