00. 00 Chào mừng đến với bài học thứ ba về Lập trình hướng đối tượng trong Python so với Java. Trong bài học trước, bạn đã khám phá cách tạo các thuộc tính thể hiện trong Python. Bây giờ chúng ta sẽ xem xét các thuộc tính của lớp
00. 14 Trong Java, chúng tôi sử dụng từ khóa static để chỉ ra một trường lớp, vì vậy câu lệnh này được đặt cùng với các định nghĩa trường khác sẽ tạo ra một trường lớp có giá trị 4 được gọi là a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 00
00. 29 Đây sẽ là cùng một giá trị được chia sẻ bởi bất kỳ thể hiện nào của lớp mà chúng ta tạo. Trong Python, các thuộc tính lớp được tạo bằng cách định nghĩa một biến bên ngoài bất kỳ phương thức nào, thường xảy ra trước bất kỳ phương thức nào
00. 48 Vì vậy, ở đây, trong phiên bản mới này của lớp Python a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 01 của tôi, tôi đã tạo một thuộc tính lớp có tên là a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 00 và tôi đã gán cho nó một giá trị là 4. Chúng tôi không làm bất cứ điều gì trong phương pháp a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 04
01. 03 Chúng tôi sẽ không làm bất cứ điều gì trong hàm tạo cho một lớp Java. Nhưng bây giờ điều này đưa ra một thuộc tính mà tất cả các đối tượng của lớp a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 01 sẽ chia sẻ
01. 16 Để xem nó hoạt động như thế nào, hãy tiếp tục và tạo một a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 01
01. 23 Và giống như các thuộc tính thể hiện, tôi sử dụng toán tử dấu chấm (_______17) để truy cập thuộc tính lớp. Vì vậy, tôi sẽ nói 40, và sau đó tôi có thể nói 41
01. 45 Và 42. Nếu bạn chưa tạo bất kỳ đối tượng nào, bạn có thể tham khảo lớp bằng cách trước tiên nói tên tệp—tên mô-đun, chính xác hơn…
02. 05 Vì vậy, chúng ta có thể nói 43. Trong Python, một đối tượng thực sự có thể thay đổi giá trị của thuộc tính lớp của chính nó, không ảnh hưởng đến thuộc tính của các đối tượng khác đã được tạo hoặc chưa được tạo
02. 26 Vì vậy, hãy để tôi làm lại chiếc xe bán tải của mình
02. 32 Tôi có một chiếc Chevy S-10 màu đỏ. Tôi có thể nói 44. Bây giờ, giả sử chúng ta chuyển đổi cái này thành một cặp đôi. Tôi sẽ nói 45
02. 55 Tôi không khuyến nghị, nhưng chúng ta có thể gán lại giá trị của thuộc tính lớp cho một đối tượng cụ thể. Vì vậy, bây giờ tôi có thể thực hiện câu lệnh in cuối cùng đó, 46…
03. 14 và bây giờ nó có 6 bánh xe, nhưng 47
03. 25 còn 4 bánh. Trong bài học này, chúng ta đã học về các thuộc tính của lớp. Trong bài học tiếp theo, chúng ta sẽ tìm hiểu cách Python xử lý khả năng hiển thị khi mọi thứ trong Python được công khai
Trong thế giới lập trình hiện đại, các ngôn ngữ lập trình hướng đối tượng (OOP) đã đóng một vai trò tiến hóa trong việc thay đổi các mẫu thiết kế và triển khai trong phát triển phần mềm. Là một thành viên quan trọng của gia đình OOP, Python có…
Trước khi so sánh và ví dụ về các thuộc tính lớp và đối tượng, trước tiên hãy định nghĩa chúng -
- Thuộc tính lớp là một biến thuộc về một lớp nhất định chứ không phải một đối tượng cụ thể. Mọi thể hiện của lớp này chia sẻ cùng một biến. Các thuộc tính này thường được định nghĩa bên ngoài hàm tạo a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 06 - Thuộc tính thể hiện/đối tượng là một biến thuộc về một (và chỉ một) đối tượng. Mỗi thể hiện của một lớp trỏ đến các biến thuộc tính của chính nó. Các thuộc tính này được xác định trong hàm tạo a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 06
Tại sao người ta cần sử dụng thuộc tính lớp và thuộc tính đối tượng?
Trong một thế giới song song chỉ toàn những chú chó, mỗi chú chó đều có tên và tuổi. Tổng số chó phải được cập nhật mọi lúc. Tất cả điều này phải được xác định trong một lớp. Điều này có thể trông giống như thế này
class Dog:dogs_count = 0 def __init__(self, name, age):
self.name = name
self.age = age
print("Welcome to this world {}!".format(self.name))
Dog.dogs_count += 1 def __del__(self):
print("Goodbye {} :(".format(self.name))
Dog.dogs_count -= 1
Trong lớp này, chúng tôi có một thuộc tính lớp a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 08. Biến này theo dõi số lượng chó chúng ta có trong thế giới chó của chúng ta. Chúng tôi có hai thuộc tính thể hiện, a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 09 và a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 00. Các biến này là duy nhất cho mỗi con chó (các thuộc tính của mỗi phiên bản có các vị trí bộ nhớ khác nhau). Mỗi khi hàm a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 06 được thực thi, thì a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 08 sẽ tăng lên. Tương tự như vậy — mỗi khi một con chó chết (tiếc là chó không sống mãi trên đời này), gọi phương thức a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 02, thì a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 08 sẽ giảm
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 0
á ha. Chúng tôi quản lý để gán các biến duy nhất cho một đối tượng trong khi có một biến chung mà tất cả các đối tượng chứa
Kế thừa các thuộc tính
Trước khi mở chủ đề này, chúng ta hãy xem thuộc tính tích hợp sẵn a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 04
classAttr = 0
def __init__(self, instanceAttr):
self.instanceAttr = instanceAttra = Example(1)
print(a.__dict__)
print(Example.__dict__)Output:
{'instanceAttr': 1}
{'__module__': '__main__', '__doc__': None, '__dict__': <attribute '__dict__' of 'Example' objects>, '__init__': <function Example.__init__ at 0x7f8af2113f28>, 'classAttr': 0, '__weakref__': <attribute '__weakref__' of 'Example' objects>}
Như chúng ta có thể thấy, cả lớp và đối tượng đều có một từ điển với các khóa và giá trị thuộc tính. Từ điển lớp lưu trữ nhiều thuộc tính tích hợp mà một phiên bản không chứa
b = Example(2)print(b.classAttr)
print(Example.classAttr)
b.classAttr = 653
print(b.classAttr)
print(Example.classAttr)Output:
0
0
653
0
TUYỆT VỜI. Quay trở lại những gì tôi đã viết trước đó, mỗi phiên bản của một lớp chia sẻ các thuộc tính lớp giống nhau. Điều gì đã xảy ra ở đây? . Nhìn vào từ điển của các yếu tố này sẽ cung cấp cho cái nhìn sâu sắc hơn
b = Example(2)print(b.__dict__)
print(Example.__dict__)
b.classAttr = 653
print(b.__dict__)
print(Example.__dict__)Output:
{'instanceAttr': 2}
'__module__': '__main__', '__doc__': None, '__dict__': <attribute '__dict__' of 'Example' objects>, '__init__': <function Example.__init__ at 0x7f8af2113f28>, 'classAttr': 0, '__weakref__': <attribute '__weakref__' of 'Example' objects>}
{'instanceAttr': 2, 'classAttr': 653}
{'__module__': '__main__', '__doc__': None, '__dict__': <attribute '__dict__' of 'Example' objects>, '__init__': <function Example.__init__ at 0x7f8af2113f28>, 'classAttr': 0, '__weakref__': <attribute '__weakref__' of 'Example' objects>}
Quan sát kỹ, chúng tôi nhận thấy rằng a = Dog("Max", 1)
print("Number of dogs: {}".format(Dog.dogs_count))
b = Dog("Charlie", 7)
del a
c = Dog("Spot", 4.5)
print("Number of dogs: {}".format(Dog.dogs_count))
del b
del c
print("Number of dogs: {}".format(Dog.dogs_count))Output:
Welcome to this world Max!
Number of dogs: 1
Welcome to this world Charlie!
Goodbye Max :(
Welcome to this world Spot!
Number of dogs: 2
Goodbye Charlie :(
Goodbye Spot :(
Number of dogs: 05 đã được thêm vào từ điển của đối tượng, với giá trị được sửa đổi. Từ điển của lớp vẫn giữ nguyên, điều này cho thấy rằng các thuộc tính của lớp đôi khi có thể hoạt động như các thuộc tính thể hiện
Tóm lại, các thuộc tính lớp và đối tượng cực kỳ hữu ích nhưng có thể trở nên lộn xộn khi được sử dụng cùng nhau. Thuộc tính lớp thuận lợi khi mỗi đối tượng cần chia sẻ một biến, chẳng hạn như bộ đếm. Các thuộc tính đối tượng có lợi thế khi mỗi đối tượng duy nhất cần các giá trị riêng của nó, điều gì đó khiến chúng khác biệt với các đối tượng khác