Mặc dù Python không hoàn toàn là một ngôn ngữ hướng đối tượng, nhưng nó đủ linh hoạt và đủ mạnh để cho phép bạn xây dựng các ứng dụng của mình bằng mô hình hướng đối tượng. Một trong những cách mà Python đạt được điều này là bằng cách hỗ trợ tính kế thừa, điều mà nó thực hiện với
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0Trong hướng dẫn này, bạn sẽ tìm hiểu về những điều sau đây
- Khái niệm kế thừa trong Python
- Đa kế thừa trong Python
- Hàm
0 hoạt động như thế nàoclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
- Cách thức hoạt động của hàm
0 trong thừa kế đơn lẻclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
- Hàm
0 trong đa thừa kế hoạt động như thế nàoclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới
Tổng quan về Hàm class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 của Python
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Nếu bạn có kinh nghiệm với các ngôn ngữ hướng đối tượng, bạn có thể đã quen thuộc với chức năng của
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0Nếu không, đừng sợ. Mặc dù khá kỹ thuật, nhưng ở cấp độ cao,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 cho phép bạn truy cập vào các phương thức trong một lớp cha từ lớp con kế thừa từ nóRiêng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 trả về một đối tượng tạm thời của siêu lớp mà sau đó cho phép bạn gọi các phương thức của siêu lớp đóTại sao bạn muốn làm bất kỳ điều này?
Việc gọi các phương thức đã tạo trước đó bằng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 giúp bạn không cần phải viết lại các phương thức đó trong lớp con của mình và cho phép bạn hoán đổi các lớp cha với các thay đổi mã tối thiểuLoại bỏ các quảng cáoclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 trong Kế thừa đơn
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Nếu bạn chưa quen với các khái niệm lập trình hướng đối tượng, thì kế thừa có thể là một thuật ngữ xa lạ. Kế thừa là một khái niệm trong lập trình hướng đối tượng, trong đó một lớp dẫn xuất [hoặc kế thừa] các thuộc tính và hành vi từ một lớp khác mà không cần thực hiện lại chúng
Ít nhất đối với tôi, việc hiểu các khái niệm này khi xem mã sẽ dễ dàng hơn, vì vậy hãy viết các lớp mô tả một số hình dạng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0Ở đây, có hai lớp tương tự.
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11Bạn có thể sử dụng chúng như dưới đây
>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
3Trong ví dụ này, bạn có hai hình có liên quan với nhau. hình vuông là một loại hình chữ nhật đặc biệt. Tuy nhiên, mã không phản ánh mối quan hệ đó và do đó có mã về cơ bản được lặp lại
Bằng cách sử dụng tính kế thừa, bạn có thể giảm số lượng mã bạn viết đồng thời phản ánh mối quan hệ trong thế giới thực giữa hình chữ nhật và hình vuông
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Ở đây, bạn đã sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 để gọi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
13 của lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10, cho phép bạn sử dụng nó trong lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 mà không cần lặp lại mã. Dưới đây, chức năng cốt lõi vẫn còn sau khi thực hiện thay đổi>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
1Trong ví dụ này,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 là lớp cha và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 là lớp conBởi vì các phương thức
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 rất giống nhau, nên bạn có thể chỉ cần gọi phương thức class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 của siêu lớp [class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
42] từ phương thức của class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 bằng cách sử dụng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0. Điều này đặt các thuộc tính class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
45 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
46 mặc dù bạn chỉ cần cung cấp một tham số class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
47 duy nhất cho hàm tạo class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11Khi bạn chạy cái này, mặc dù lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 của bạn không triển khai nó một cách rõ ràng, lệnh gọi tới class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 sẽ sử dụng phương thức class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 trong lớp cha và in ra class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
42. Lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 kế thừa lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 từ lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10Ghi chú. Để tìm hiểu thêm về các khái niệm kế thừa và hướng đối tượng trong Python, hãy nhớ xem Kế thừa và Thành phần. Hướng dẫn OOP Python và Lập trình hướng đối tượng [OOP] trong Python 3
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 có thể làm gì cho bạn?
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Vì vậy,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 có thể làm gì cho bạn trong tài sản thừa kế duy nhất?Giống như các ngôn ngữ hướng đối tượng khác, nó cho phép bạn gọi các phương thức của lớp cha trong lớp con của bạn. Trường hợp sử dụng chính của điều này là để mở rộng chức năng của phương thức kế thừa
Trong ví dụ bên dưới, bạn sẽ tạo một lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 kế thừa từ lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 và mở rộng chức năng của lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 [kế thừa từ lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 đến lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11] để tính diện tích bề mặt và thể tích của một thực thể class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
4Bây giờ bạn đã xây dựng các lớp, hãy xem diện tích bề mặt và thể tích của hình lập phương có chiều dài cạnh là
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
94>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
4thận trọng. Lưu ý rằng trong ví dụ của chúng tôi ở trên, một mình
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 sẽ không thực hiện cuộc gọi phương thức cho bạn. bạn phải gọi phương thức trên chính đối tượng proxyỞ đây bạn đã triển khai hai phương thức cho lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48. class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
97 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
98. Cả hai phép tính này đều dựa trên việc tính diện tích của một mặt, do đó, thay vì thực hiện lại phép tính diện tích, bạn sử dụng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 để mở rộng phép tính diện tíchCũng lưu ý rằng định nghĩa lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 không có class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40. Bởi vì class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 kế thừa từ class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 không thực sự làm bất cứ điều gì khác biệt cho class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 so với những gì đã làm cho class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11, bạn có thể bỏ qua việc xác định nó và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 của siêu lớp [class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11] sẽ được gọi tự độngclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 trả về một đối tượng ủy nhiệm cho một lớp cha, vì vậy bạn gọi phương thức bạn muốn trực tiếp trên nó. class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
60Điều này không chỉ giúp chúng tôi không phải viết lại các phép tính diện tích mà còn cho phép chúng tôi thay đổi logic
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 bên trong ở một vị trí duy nhất. Điều này đặc biệt hữu ích khi bạn có một số lớp con kế thừa từ một lớp chaLoại bỏ các quảng cáoMột chuyến lặn sâu class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Trước khi đi vào đa thừa kế, chúng ta hãy tìm hiểu nhanh về cơ chế của
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0Trong khi các ví dụ trên [và bên dưới] gọi
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 mà không có bất kỳ tham số nào, thì class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 cũng có thể nhận hai tham số. đầu tiên là lớp con và tham số thứ hai là một đối tượng là một thể hiện của lớp con đóĐầu tiên, hãy xem hai ví dụ cho thấy việc thao tác với biến đầu tiên có thể làm gì, bằng cách sử dụng các lớp đã được hiển thị
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
9Trong Python 3, lệnh gọi
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
66 tương đương với lệnh gọi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 không tham số. Tham số đầu tiên đề cập đến phân lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11, trong khi tham số thứ hai đề cập đến một đối tượng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11, trong trường hợp này là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
70. Bạn cũng có thể gọi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 với các lớp khácclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
9Trong ví dụ này, bạn đang đặt
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 làm đối số phân lớp thành class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0, thay vì class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48. Điều này khiến class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 bắt đầu tìm kiếm một phương thức so khớp [trong trường hợp này là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40] ở một cấp trên class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 trong hệ thống phân cấp cá thể, trong trường hợp này là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10Trong ví dụ cụ thể này, hành vi không thay đổi. Nhưng hãy tưởng tượng rằng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 cũng đã triển khai một hàm class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 mà bạn muốn đảm bảo rằng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 không sử dụng. Gọi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 theo cách này cho phép bạn làm điều đóthận trọng. Mặc dù chúng tôi đang loay hoay với các tham số của
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 để khám phá cách thức hoạt động ẩn của nó, nhưng tôi thận trọng không nên làm điều này thường xuyênCuộc gọi không tham số tới
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 được khuyến nghị và đủ cho hầu hết các trường hợp sử dụng và việc cần thay đổi cấu trúc phân cấp tìm kiếm thường xuyên có thể là dấu hiệu của một vấn đề thiết kế lớn hơnCòn tham số thứ hai thì sao? . Ví dụ,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
305 phải trả về class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
306Bằng cách bao gồm một đối tượng được khởi tạo,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 trả về một phương thức ràng buộc. một phương thức được liên kết với đối tượng, cung cấp cho phương thức ngữ cảnh của đối tượng, chẳng hạn như bất kỳ thuộc tính thể hiện nào. Nếu không bao gồm tham số này, phương thức được trả về chỉ là một hàm, không được liên kết với ngữ cảnh của đối tượngĐể biết thêm thông tin về các phương thức liên kết, phương thức không liên kết và chức năng, hãy đọc tài liệu Python trên hệ thống mô tả của nó
Ghi chú. Về mặt kỹ thuật,
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 không trả về một phương thức. Nó trả về một đối tượng proxy. Đây là một đối tượng ủy nhiệm các cuộc gọi đến các phương thức lớp chính xác mà không cần tạo một đối tượng bổ sung để làm như vậyclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 trong nhiều kế thừa
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Bây giờ bạn đã xem qua phần tổng quan và một số ví dụ về
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 và thừa kế đơn lẻ, bạn sẽ được giới thiệu về tổng quan và một số ví dụ sẽ chứng minh cách thức hoạt động của đa thừa kế và cách class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 kích hoạt chức năng đóTổng quan về đa kế thừa
Có một trường hợp sử dụng khác trong đó ________ 00 thực sự tỏa sáng và trường hợp này không phổ biến như trường hợp thừa kế đơn lẻ. Ngoài tính năng kế thừa đơn lẻ, Python hỗ trợ tính năng đa kế thừa, trong đó một lớp con có thể kế thừa từ nhiều lớp cha mà không nhất thiết phải kế thừa lẫn nhau [còn được gọi là lớp anh chị em]
Tôi là một người rất trực quan và tôi thấy sơ đồ cực kỳ hữu ích để hiểu các khái niệm như thế này. Hình ảnh bên dưới cho thấy một kịch bản đa kế thừa rất đơn giản, trong đó một lớp kế thừa từ hai siêu lớp [anh chị em] không liên quan
Để minh họa rõ hơn về hoạt động của đa kế thừa, đây là một số mã để bạn dùng thử, chỉ ra cách bạn có thể xây dựng một hình chóp bên phải [hình chóp có đáy hình vuông] từ một
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 và một class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
6Ghi chú. Thuật ngữ chiều cao nghiêng có thể không quen thuộc, đặc biệt nếu đã lâu rồi bạn chưa học lớp hình học hoặc làm việc trên bất kỳ kim tự tháp nào
Chiều cao xiên là chiều cao tính từ tâm đáy của một vật thể [như kim tự tháp] lên mặt của nó đến đỉnh của vật thể đó. Bạn có thể đọc thêm về độ cao nghiêng tại Wolfram MathWorld
Ví dụ này khai báo một lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 và một lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316 kế thừa từ cả hai lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313Bạn sẽ thấy một phương thức
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 khác sử dụng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 giống như trong kế thừa đơn lẻ, với mục đích là đạt tới các phương thức class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
321 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 đã được định nghĩa trong lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10Ghi chú. Bạn có thể nhận thấy rằng đoạn mã trên chưa sử dụng bất kỳ thuộc tính kế thừa nào từ lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313. Các ví dụ sau sẽ tận dụng tối đa tính kế thừa từ cả class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11Tuy nhiên, vấn đề là cả hai siêu lớp [
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11] đều định nghĩa một class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40. Hãy dành một chút thời gian và nghĩ xem điều gì có thể xảy ra khi bạn gọi cho class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 trên số class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316, sau đó thử gọi như bên dưới>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
7Bạn có đoán rằng Python sẽ cố gọi
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
332 không? Ghi chú. Làm thế nào chúng tôi nhận thấy rằng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
332 được gọi chứ không phải, như chúng tôi hy vọng, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
334? class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
30Bạn có thể nhận ra điều này từ lớp hình học là công thức tính diện tích tam giác. Mặt khác, nếu bạn giống tôi, bạn có thể đã cuộn đến các định nghĩa lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 và thấy mã tương tự này trong class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
332Loại bỏ các quảng cáoThứ tự giải quyết phương pháp
Thứ tự phân giải phương thức [hoặc MRO] cho Python biết cách tìm kiếm các phương thức kế thừa. Điều này rất hữu ích khi bạn đang sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 vì MRO cho bạn biết chính xác nơi Python sẽ tìm phương thức bạn đang gọi với class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 và theo thứ tự nàoMỗi lớp có một thuộc tính
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
341 cho phép chúng tôi kiểm tra thứ tự, vì vậy hãy làm điều đó>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
31Điều này cho chúng ta biết rằng các phương thức sẽ được tìm kiếm đầu tiên trong
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
342, sau đó là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313, sau đó là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11, sau đó là class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10, và sau đó, nếu không tìm thấy gì, trong class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
346, từ đó tất cả các lớp bắt nguồnVấn đề ở đây là trình thông dịch đang tìm kiếm
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 trong class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 trước class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10, và khi tìm thấy class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 trong class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313, Python gọi nó thay vì tên bạn muốn. Bởi vì class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
332 mong đợi có một thuộc tính class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
354 và một thuộc tính class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
355, nên Python ném ra một class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
335May mắn thay, bạn có một số quyền kiểm soát cách MRO được xây dựng. Chỉ bằng cách thay đổi chữ ký của lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316, bạn có thể tìm kiếm theo thứ tự bạn muốn và các phương thức sẽ giải quyết chính xácclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
32Lưu ý rằng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316 khởi tạo một phần với class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 từ lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11. Điều này cho phép class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 sử dụng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
45 trên đối tượng, như được thiết kếBây giờ, bạn có thể xây dựng một kim tự tháp, kiểm tra MRO và tính diện tích bề mặt
>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
33Bạn thấy rằng MRO bây giờ là những gì bạn mong đợi và bạn cũng có thể kiểm tra khu vực của kim tự tháp, nhờ vào
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
321Vẫn còn một vấn đề ở đây, mặc dù. Để đơn giản, tôi đã làm sai một vài điều trong ví dụ này. điều đầu tiên và được cho là quan trọng nhất là tôi có hai lớp riêng biệt có cùng tên phương thức và chữ ký
Điều này gây ra sự cố với giải pháp phương pháp, vì phiên bản đầu tiên của
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 gặp phải trong danh sách MRO sẽ được gọiKhi bạn đang sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 với nhiều kế thừa, bắt buộc phải thiết kế các lớp của bạn để hợp tác. Một phần của điều này là đảm bảo rằng các phương thức của bạn là duy nhất để chúng được giải quyết trong MRO, bằng cách đảm bảo chữ ký của phương thức là duy nhất—cho dù bằng cách sử dụng tên phương thức hay tham số phương thứcTrong trường hợp này, để tránh sửa chữa toàn bộ mã của bạn, bạn có thể đổi tên phương thức
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 của lớp class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 thành class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
369. Bằng cách này, các phương thức vùng có thể tiếp tục sử dụng các thuộc tính của lớp thay vì lấy các tham số bên ngoàiclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
34Chúng ta hãy tiếp tục và sử dụng điều này trong lớp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
35Vấn đề tiếp theo ở đây là mã không có đối tượng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313 được ủy quyền giống như đối với đối tượng class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11, vì vậy, việc gọi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
373 sẽ cho chúng ta một class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
335 vì class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
355 và class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
354 không có bất kỳ giá trị nàoBạn cần làm hai việc để khắc phục điều này
Tất cả các phương thức được gọi với
0 cần phải có một cuộc gọi đến phiên bản siêu lớp của phương thức đó. Điều này có nghĩa là bạn sẽ cần thêmclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
378 vào phương phápclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
40 củaclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
313 vàclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
10class Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
Thiết kế lại tất cả các cuộc gọi
40 để lấy từ điển từ khóa. Xem mã hoàn chỉnh bên dướiclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
Ví dụ về mã hoàn chỉnhHiển thị/Ẩn
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
36Có một số khác biệt quan trọng trong mã này
**______1383 được sửa đổi ở một số chỗ [chẳng hạn như
384]. ** Điều này sẽ cho phép người dùng của các đối tượng này chỉ khởi tạo chúng bằng các đối số có ý nghĩa đối với đối tượng cụ thể đóclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
Thiết lập các đối số được đặt tên trước
385. Bạn có thể thấy điều này trongclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
384. Điều này có tác dụng gọn gàng là lấy ngay khóa đó ra khỏi từ điểnclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
385, để khi nó kết thúc ở cuối MRO trong lớpclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
346, thìclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
385 trốngclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
Ghi chú. Theo dõi trạng thái của
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
383 ở đây có thể phức tạp, vì vậy đây là bảng gồm các lệnh gọi của class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
40 theo thứ tự, hiển thị lớp sở hữu lệnh gọi đó và nội dung của class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
383 trong suốt cuộc gọi đóClassNamed Arguments
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
383class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
316class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
395, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
396class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
11class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
47class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
395, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
00class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
47, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
03class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
395, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
00class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
313class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
395, class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
00Bây giờ, khi bạn sử dụng các lớp cập nhật này, bạn có
>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
37Nó hoạt động. Bạn đã sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 để điều hướng thành công hệ thống phân cấp lớp phức tạp trong khi sử dụng cả kế thừa và thành phần để tạo các lớp mới với việc triển khai lại tối thiểuLoại bỏ các quảng cáoNhiều lựa chọn thay thế kế thừa
Như bạn có thể thấy, đa kế thừa có thể hữu ích nhưng cũng dẫn đến các tình huống rất phức tạp và mã khó đọc. Cũng hiếm khi có các đối tượng kế thừa gọn gàng mọi thứ từ nhiều đối tượng khác
Nếu bạn thấy mình bắt đầu sử dụng đa thừa kế và phân cấp lớp phức tạp, thì bạn nên tự hỏi liệu bạn có thể đạt được mã rõ ràng và dễ hiểu hơn bằng cách sử dụng thành phần thay vì kế thừa hay không. Vì bài viết này tập trung vào tính kế thừa nên tôi sẽ không đi sâu vào chi tiết về thành phần và cách sử dụng nó trong Python. May mắn thay, Real Python đã xuất bản một hướng dẫn chuyên sâu về cả kế thừa và thành phần trong Python sẽ giúp bạn trở thành chuyên gia OOP ngay lập tức
Có một kỹ thuật khác có thể giúp bạn vượt qua sự phức tạp của đa thừa kế trong khi vẫn mang lại nhiều lợi ích. Kỹ thuật này ở dạng một lớp đơn giản, chuyên biệt được gọi là mixin
Một mixin hoạt động như một loại kế thừa, nhưng thay vì xác định mối quan hệ “is-a”, có thể chính xác hơn khi nói rằng nó xác định mối quan hệ “bao gồm-a”. Với một hỗn hợp, bạn có thể viết một hành vi có thể được đưa trực tiếp vào bất kỳ lớp nào khác
Bên dưới, bạn sẽ thấy một ví dụ ngắn sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10 để cung cấp chức năng cụ thể cho các đối tượng 3D của chúng ta—trong trường hợp này là phép tính thể tíchclass Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
38Trong ví dụ này, mã đã được làm lại để bao gồm một mixin có tên là
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
10. Mixin sau đó được sử dụng bởi class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 và cung cấp cho class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
48 khả năng tính toán khối lượng của nó, được hiển thị bên dưới>>>
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
39Mixin này có thể được sử dụng theo cách tương tự trong bất kỳ lớp nào khác có diện tích được xác định cho nó và công thức
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
14 trả về âm lượng chính xácBản tóm tắt class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
Trong hướng dẫn này, bạn đã học cách tăng tốc các lớp học của mình với
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0. Hành trình của bạn bắt đầu với việc xem xét kế thừa đơn lẻ và sau đó chỉ ra cách gọi các phương thức siêu lớp một cách dễ dàng với class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0Sau đó, bạn đã học cách hoạt động của đa kế thừa trong Python và các kỹ thuật để kết hợp
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0 với đa kế thừa. Bạn cũng đã tìm hiểu về cách Python giải quyết các cuộc gọi phương thức bằng cách sử dụng thứ tự phân giải phương thức [MRO], cũng như cách kiểm tra và sửa đổi MRO để đảm bảo các phương thức thích hợp được gọi vào những thời điểm thích hợpĐể biết thêm thông tin về lập trình hướng đối tượng trong Python và sử dụng
class Rectangle:
def __init__[self, length, width]:
self.length = length
self.width = width
def area[self]:
return self.length * self.width
def perimeter[self]:
return 2 * self.length + 2 * self.width
# Here we declare that the Square class inherits from the Rectangle class
class Square[Rectangle]:
def __init__[self, length]:
super[].__init__[length, length]
0, hãy xem các tài nguyên này
0 của Python Được coi là Siêu nhân của Raymond Hettingerclass Rectangle: def __init__[self, length, width]: self.length = length self.width = width def area[self]: return self.length * self.width def perimeter[self]: return 2 * self.length + 2 * self.width # Here we declare that the Square class inherits from the Rectangle class class Square[Rectangle]: def __init__[self, length]: super[].__init__[length, length]
- Lập trình hướng đối tượng trong Python 3
Đánh dấu là đã hoàn thành
Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Nâng cấp lớp học của bạn với Python super[]
🐍 Thủ thuật Python 💌
Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python
Gửi cho tôi thủ thuật Python »
Giới thiệu về Kyle Stratis
Kyle là một nhà phát triển tự học đang làm kỹ sư dữ liệu cấp cao tại Vizit Labs. Trong quá khứ, ông đã thành lập DanqEx [trước đây là Nasdanq. sàn giao dịch chứng khoán meme ban đầu] và Encryptid Gaming
» Thông tin thêm về KyleMỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là
Aldren
Geir Arne
Joanna
Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonista chuyên gia
Nâng cao kỹ năng Python của bạn »
Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia
Nâng cao kỹ năng Python của bạn »
Bạn nghĩ sao?
Đánh giá bài viết này
Tweet Chia sẻ Chia sẻ EmailBài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?
Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi