Mỗi giá trị trong Python là một đối tượng. Bất kỳ đối tượng nào bạn tạo dựa trên một lớp mà bạn tự xác định đều hoạt động giống hệt như bất kỳ đối tượng Python "thông thường" nào. Ví dụ, các đối tượng có thể được lưu trữ trong một danh sách
from datetime import date
class CompletedCourse:
def __init__[self, course_name: str, credits: int, completion_date: date]:
self.name = course_name
self.credits = credits
self.completion_date = completion_date
if __name__ == "__main__":
# Here we create some completed courses and add these to a list
completed = []
maths1 = CompletedCourse["Mathematics 1", 5, date[2020, 3, 11]]
prog1 = CompletedCourse["Programming 1", 6, date[2019, 12, 17]]
completed.append[maths1]
completed.append[prog1]
# Let's add a couple more straight to the list
completed.append[CompletedCourse["Physics 2", 4, date[2019, 11, 10]]]
completed.append[CompletedCourse["Programming 2", 5, date[2020, 5, 19]]]
# Go through all the completed courses, print out their names
# and sum up the credits received
credits = 0
for course in completed:
print[course.name]
credits += course.credits
print["Total credits received:", credits]
đầu ra mẫu
Toán 1 Lập trình 1 Vật lý 2 Lập trình 2 Tổng số tín chỉ nhận được. 20
Đang tải
Đang tải
Bạn có thể nhớ rằng bản thân các danh sách không chứa bất kỳ đối tượng nào. Chúng chứa các tham chiếu đến các đối tượng. Cùng một đối tượng có thể xuất hiện nhiều lần trong một danh sách và nó có thể được tham chiếu nhiều lần trong hoặc ngoài danh sách. Hãy xem một ví dụ
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
Nếu có nhiều hơn một tham chiếu đến cùng một đối tượng, thì việc sử dụng một trong các tham chiếu đó không có gì khác biệt
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
đầu ra mẫu
Chó ban đầu. Fluffy Fluffy Fluffy Con chó ở chỉ số 0 được đổi tên. Pooch Pooch Fluffy Con chó ở chỉ số 2 được đổi tên. Chú chó chú chó Fifi
Các tham chiếu tại chỉ mục 0 và 1 trong danh sách đề cập đến cùng một đối tượng. Một trong các tham chiếu có thể được sử dụng để truy cập đối tượng. Tham chiếu tại chỉ mục 2 đề cập đến một đối tượng khác, mặc dù có cùng nội dung. Thay đổi nội dung của đối tượng sau này không ảnh hưởng đến đối tượng kia
Toán tử
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
0 được sử dụng để kiểm tra xem hai tham chiếu có đề cập đến cùng một đối tượng hay không, trong khi toán tử class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
1 sẽ cho bạn biết nội dung của các đối tượng có giống nhau không. Ví dụ sau đây hy vọng làm cho sự khác biệt rõ rànglist1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
đầu ra mẫu
Sai Đúng Sai
Đúng Đúng Đúng
Bất kỳ đối tượng Python nào cũng có thể được lưu trữ trong từ điển hoặc bất kỳ cấu trúc dữ liệu nào khác. Điều này cũng áp dụng cho các đối tượng được tạo dựa trên lớp mà bạn đã tự xác định
class Student:
def __init__[self, name: str, cr: int]:
self.name = name
self.cr = cr
if __name__ == "__main__":
# The key in this dictionary is the student number,
# and the value is an object of type Student
students = {}
students["12345"] = Student["Saul Student", 10]
students["54321"] = Student["Sally Student", 67]
Có thể giúp hiểu ý nghĩa của ví dụ trên
Ngã hay vô ngã?
Cho đến nay, chúng ta mới chỉ chạm vào bề mặt của việc sử dụng tên tham số
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2. Chúng ta hãy xem xét kỹ hơn khi nào nên hoặc không nên sử dụngDưới đây chúng ta có một lớp đơn giản cho phép chúng ta tạo một đối tượng từ vựng chứa một số từ
________số 8
đầu ra mẫu
mọt sách lập trình hướng đối tượng python
Danh sách các từ được lưu trữ trong một thuộc tính có tên là
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
3. Trong trường hợp này, tên tham số class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2 là bắt buộc trong cả phương thức khởi tạo của lớp và trong bất kỳ phương thức nào khác truy cập vào biến đó. Nếu bỏ qua class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2, các phương thức khác nhau sẽ không truy cập cùng một danh sách các từHãy thêm một phương thức mới vào định nghĩa lớp của chúng ta. Phương thức
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
6 trả về [một trong những] từ dài nhất trong từ vựngSau đây là một cách để hoàn thành nhiệm vụ này, nhưng chúng ta sẽ sớm thấy đó không phải là cách tốt
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
3Phương thức này sử dụng hai biến trợ giúp được khai báo với tên tham số
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2. Hãy nhớ rằng, tên của các biến không quan trọng theo nghĩa chức năng, vì vậy các biến này cũng có thể được đặt tên khó hiểu hơn, chẳng hạn như class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
8 và class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
9. Mã bắt đầu trông hơi khó hiểuclass Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
7Khi một biến được khai báo với tên tham số
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2, nó sẽ trở thành một thuộc tính của đối tượng. Điều này có nghĩa là biến sẽ tồn tại chừng nào đối tượng còn tồn tại. Cụ thể, biến sẽ tiếp tục tồn tại sau khi phương thức khai báo nó kết thúc quá trình thực thi. Trong ví dụ trên, điều này là hoàn toàn không cần thiết, vì các biến của trình trợ giúp chỉ được sử dụng trong phương thức class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
6. Vì vậy, khai báo các biến trợ giúp với tên tham số class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2 không phải là một ý tưởng hay ở đâyBên cạnh việc khiến các biến tồn tại sau "ngày hết hạn" của chúng, việc sử dụng
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2 để tạo các thuộc tính mới khi chúng không cần thiết có thể gây ra các lỗi khó khắc phục trong mã của bạn. Đặc biệt là các thuộc tính được đặt tên chung chung như list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
4, sau đó được sử dụng trong nhiều phương thức khác nhau, có thể gây ra hành vi không mong muốn và khó theo dõiVí dụ: nếu một biến trợ giúp được khai báo là một thuộc tính và được gán một giá trị ban đầu trong hàm tạo, nhưng biến này sau đó được sử dụng trong một ngữ cảnh không liên quan trong một phương thức khác, thì kết quả thường không thể đoán trước
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
3Bạn có thể nghĩ rằng vấn đề này sẽ được giải quyết bằng cách chỉ khai báo các thuộc tính nơi chúng được sử dụng, bên ngoài hàm tạo, nhưng điều này dẫn đến tình huống các thuộc tính có thể truy cập thông qua một đối tượng phụ thuộc vào phương thức nào đã được thực thi. Trong phần trước, chúng ta đã thấy rằng lợi thế của việc khai báo các thuộc tính trong hàm tạo là tất cả các thể hiện của lớp sau đó sẽ có các thuộc tính giống hệt nhau. Nếu không đúng như vậy, việc sử dụng các thể hiện khác nhau của lớp có thể dễ dẫn đến lỗi
Tóm lại, nếu bạn cần các biến trợ giúp để sử dụng trong một phương thức duy nhất, thì cách chính xác để thực hiện là không có
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2. Để làm cho mã của bạn dễ hiểu hơn, hãy sử dụng các tên biến thông tinclass Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
5Trong cách triển khai ở trên, các biến của trình trợ giúp chỉ có thể truy cập được trong khi phương thức đang được thực thi. Các giá trị được lưu trữ bên trong không thể gây phức tạp trong các phần khác của chương trình
Đối tượng làm đối số cho hàm
Các đối tượng được tạo dựa trên các lớp của chúng ta thường có thể thay đổi. Bạn có thể nhớ rằng, ví dụ, danh sách Python có thể thay đổi. khi được truyền dưới dạng đối số cho hàm, nội dung của chúng có thể thay đổi do quá trình thực thi
Chúng ta hãy xem một ví dụ đơn giản trong đó một hàm nhận tham chiếu đến một đối tượng kiểu
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
6 làm đối số của nó. Hàm sau đó thay đổi tên của sinh viên. Cả chức năng và chức năng chính gọi nó đều truy cập vào cùng một đối tượng, do đó, sự thay đổi cũng rõ ràng trong chức năng chínhclass Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
0đầu ra mẫu
Sinh viên Steve [12345] Sinh viên Saul [12345]
Cũng có thể tạo các đối tượng trong các chức năng. Nếu một hàm trả về một tham chiếu đến đối tượng mới được tạo, thì nó cũng có thể truy cập được trong hàm chính
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
1Thực hiện như trên có thể dẫn đến bản in sau [NB. vì tính ngẫu nhiên có liên quan, nếu bạn tự mình thử mã, kết quả có thể sẽ khác]
đầu ra mẫu
Mary Rusty [78218] Mindy Rusty [80068] Mike Pythons [70396] Mark Javanese [83307] Mary Pythons [45149]
Các đối tượng làm đối số cho các phương thức
Tương tự, các đối tượng có thể đóng vai trò là đối số cho các phương thức. Hãy xem một ví dụ từ một công viên giải trí
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
2Attraction chứa một phương thức
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
7, lấy một đối tượng kiểu list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
8 làm đối số. Nếu khách đủ cao, họ sẽ được nhận lên tàu và số lượng khách sẽ tăng lên. Các lớp có thể được kiểm tra như sauclass Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
3đầu ra mẫu
Jared đã lên tàu Alice quá thấp. [ Tàu lượn siêu tốc [1 du khách]
Đang tải
Đang tải
Một thể hiện của cùng một lớp làm đối số cho một phương thức
Dưới đây chúng tôi có một phiên bản khác của lớp
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print[list1 is list2]
print[list1 is list3]
print[list2 is list3]
print[]
print[list1 == list2]
print[list1 == list3]
print[list2 == list3]
8class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
4Giả sử chúng ta muốn viết một chương trình so sánh tuổi của các đối tượng kiểu Person. Chúng ta có thể viết một chức năng riêng cho mục đích này
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
5đầu ra mẫu
Muhammad ibn Musa al-Khwarizmi lớn hơn Blaise Pascal Grace Hopper không lớn hơn Blaise Pascal
Một trong những nguyên tắc của lập trình hướng đối tượng là bao gồm bất kỳ chức năng nào xử lý các đối tượng thuộc một loại nhất định trong định nghĩa lớp, dưới dạng các phương thức. Vì vậy, thay vì một hàm, chúng ta có thể viết một phương thức cho phép chúng ta so sánh tuổi của một đối tượng Person với một đối tượng Person khác
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
6Ở đây, đối tượng mà phương thức được gọi được gọi là
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2, trong khi đối tượng Person khác là class Student:
def __init__[self, name: str, cr: int]:
self.name = name
self.cr = cr
if __name__ == "__main__":
# The key in this dictionary is the student number,
# and the value is an object of type Student
students = {}
students["12345"] = Student["Saul Student", 10]
students["54321"] = Student["Sally Student", 67]
1Hãy nhớ rằng, gọi một phương thức khác với gọi một hàm. Một phương thức được gắn vào một đối tượng với ký hiệu dấu chấm
class Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
7Ở bên trái của dấu chấm là chính đối tượng, được gọi là
class Dog:
def __init__[self, name]:
self.name = name
def __str__[self]:
return self.name
dogs = []
fluffy = Dog["Fluffy"]
dogs.append[fluffy]
dogs.append[fluffy]
dogs.append[Dog["Fluffy"]]
print["Dogs initially:"]
for dog in dogs:
print[dog]
print["The dog at index 0 is renamed:"]
dogs[0].name = "Pooch"
for dog in dogs:
print[dog]
print["The dog at index 2 is renamed:"]
dogs[2].name = "Fifi"
for dog in dogs:
print[dog]
2 trong định nghĩa phương thức. Trong ngoặc đơn là đối số của phương thức, là đối tượng được gọi là class Student:
def __init__[self, name: str, cr: int]:
self.name = name
self.cr = cr
if __name__ == "__main__":
# The key in this dictionary is the student number,
# and the value is an object of type Student
students = {}
students["12345"] = Student["Saul Student", 10]
students["54321"] = Student["Sally Student", 67]
1Bản in ra từ chương trình hoàn toàn giống với cách thực hiện chức năng trên
Một điểm khá thẩm mỹ để kết thúc. cấu trúc
class Student:
def __init__[self, name: str, cr: int]:
self.name = name
self.cr = cr
if __name__ == "__main__":
# The key in this dictionary is the student number,
# and the value is an object of type Student
students = {}
students["12345"] = Student["Saul Student", 10]
students["54321"] = Student["Sally Student", 67]
4 trong phương pháp class Student:
def __init__[self, name: str, cr: int]:
self.name = name
self.cr = cr
if __name__ == "__main__":
# The key in this dictionary is the student number,
# and the value is an object of type Student
students = {}
students["12345"] = Student["Saul Student", 10]
students["54321"] = Student["Sally Student", 67]
5 nói chung là không cần thiết. Giá trị của biểu thức Boolean trong điều kiện đã là giá trị thực chính xác được trả về. Phương pháp do đó có thể được đơn giản hóaclass Product:
def __init__[self, name: int, unit: str]:
self.name = name
self.unit = unit
if __name__ == "__main__":
shopping_list = []
milk = Product["Milk", "litre"]
shopping_list.append[milk]
shopping_list.append[milk]
shopping_list.append[Product["Cucumber", "piece"]]
8Như đã nêu trong các nhận xét trong các ví dụ trên, nếu tham số trong định nghĩa phương thức cùng kiểu với chính lớp đó, thì gợi ý kiểu phải được đặt trong dấu ngoặc kép. Việc bỏ dấu ngoặc kép sẽ gây ra lỗi, bạn sẽ thấy lỗi này nếu thử cách sau