Trong hướng dẫn trước, chúng ta đã học về Kế thừa và cách một lớp con có thể kế thừa lớp cha để sử dụng các thuộc tính và chức năng của nó
Nếu một lớp muốn kế thừa nhiều hơn một lớp thì sao?
Trong Python, có hai loại Kế thừa
- Đa thừa kế
- Kế thừa đa cấp
Python - Đa kế thừa
Đa kế thừa có nghĩa là bạn đang kế thừa thuộc tính của nhiều lớp thành một. Trong trường hợp bạn có hai lớp, chẳng hạn như
class A:
# properties of class A
class B[A]:
# class B inheriting property of class A
# more properties of class B
class C[B]:
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
5 và class A:
# properties of class A
class B[A]:
# class B inheriting property of class A
# more properties of class B
class C[B]:
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
6, và bạn muốn tạo một lớp mới kế thừa các thuộc tính của cả hai lớp class A:
# properties of class A
class B[A]:
# class B inheriting property of class A
# more properties of class B
class C[B]:
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
5 và class A:
# properties of class A
class B[A]:
# class B inheriting property of class A
# more properties of class B
class C[B]:
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
6, thìclass A:
# variable of class A
# functions of class A
class B:
# variable of class A
# functions of class A
class C[A, B]:
# class C inheriting property of both class A and B
# add more properties to class C
Vì vậy, giống như một đứa trẻ thừa hưởng các đặc điểm từ cả cha và mẹ, trong python, chúng ta có thể kế thừa nhiều lớp trong một lớp con duy nhất
Thí dụ
Hãy tạo một lớp cha có tên là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
17 và một lớp con có tên là class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
18 để thực hiện kế thừa đơnclass A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
1đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
4Đa thừa kế
Trong đa kế thừa, một lớp con có thể kế thừa từ nhiều lớp cha. Vì vậy, đây là một lớp con và nhiều lớp cha
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
5đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
6Trong ví dụ trên, chúng tôi đã tạo hai lớp cha lần lượt là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
19 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
40. Sau đó, chúng tôi tạo một đứa trẻ được gọi là class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
41 kế thừa từ các lớp Người và Công tykế thừa đa cấp
Trong kế thừa đa cấp, một lớp kế thừa từ một lớp con hoặc lớp dẫn xuất. Giả sử ba lớp A, B, C. A là lớp cha, B là lớp con của A, C là lớp con của B. Nói cách khác, chúng ta có thể nói một chuỗi các lớp được gọi là kế thừa đa cấp
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
0đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
1Trong ví dụ trên, chúng ta có thể thấy có ba lớp có tên là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
42, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
44. class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
42 là lớp cha, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43 là lớp con của Xe, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
44 là lớp con của class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43. Vì vậy, chúng ta có thể thấy chuỗi các lớpKế thừa phân cấp
Trong kế thừa phân cấp, nhiều hơn một lớp con được bắt nguồn từ một lớp cha. Nói cách khác, chúng ta có thể nói một lớp cha và nhiều lớp con
Thí dụ
Hãy tạo 'Vehicle' làm lớp cha và hai lớp con 'Car' và 'Truck' làm lớp cha
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
9đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
0Kế thừa lai
Khi thừa kế bao gồm nhiều loại hoặc kết hợp các thừa kế khác nhau được gọi là thừa kế lai
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
1Ghi chú. Trong ví dụ trên, tồn tại tính chất phân cấp và đa kế thừa. Ở đây chúng ta đã tạo, lớp cha
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
42 và hai lớp con có tên là class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
51, đây là sự kế thừa có thứ bậcMột cái khác là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
44 kế thừa từ hai lớp cha có tên là class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
42. Đây là đa thừa kếHàm class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
55 của Python
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
Khi một lớp kế thừa tất cả các thuộc tính và hành vi từ lớp cha được gọi là kế thừa. Trong trường hợp như vậy, lớp kế thừa là lớp con và lớp sau là lớp cha
Ở lớp con, chúng ta có thể tham chiếu đến lớp cha bằng cách sử dụng hàm
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
55. Hàm siêu trả về một đối tượng tạm thời của lớp cha cho phép chúng ta gọi một phương thức của lớp cha bên trong một phương thức của lớp conLợi ích của việc sử dụng hàm
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
55- Chúng tôi không bắt buộc phải nhớ hoặc chỉ định tên
58 gốc để truy cập các phương thức của nóclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
- Chúng ta có thể sử dụng hàm
55 trong cả kế thừa đơn và đa thừa kếclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
- Hàm
55 hỗ trợ khả năng sử dụng lại mã vì không cần phải viết toàn bộ hàmclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
10đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
11Trong ví dụ trên, chúng ta tạo một lớp cha
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
40 và lớp con class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
41. Trong lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
41, chúng ta gọi phương thức của lớp cha bằng cách sử dụng hàm class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
55issubclass[]
Trong Python, chúng ta có thể xác minh xem một lớp cụ thể có phải là lớp con của lớp khác hay không. Với mục đích này, chúng ta có thể sử dụng hàm tích hợp Python
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
65. Hàm này trả về class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
66 nếu lớp đã cho là lớp con của lớp đã chỉ định. Nếu không, nó sẽ trả về class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
67cú pháp
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
12Ở đâu,
58. lớp cần kiểm traclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
69. mộtclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
58, loại hoặc mộtclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
01 của các lớp hoặc kiểu dữ liệuclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
13Ngoài ra, hãy xem Python isinstance[]
Ghi đè phương thức
Trong kế thừa, tất cả các thành viên có sẵn trong lớp cha theo mặc định có sẵn trong lớp con. Nếu lớp con không thỏa mãn với việc triển khai của lớp cha, thì lớp con được phép định nghĩa lại phương thức đó bằng cách mở rộng các hàm bổ sung trong lớp con. Khái niệm này được gọi là ghi đè phương thức
Khi một phương thức của lớp con có cùng tên, cùng tham số và cùng kiểu trả về với phương thức trong lớp cha của nó, thì phương thức trong lớp con được cho là sẽ ghi đè lên phương thức trong lớp cha
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
14đầu ra
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
15Trong ví dụ trên, chúng ta tạo 2 lớp có tên là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
42 [Lớp cha] và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
43 [Lớp con]. Lớp Car mở rộng từ lớp Vehicle nên tất cả các thuộc tính của lớp cha đều có sẵn trong lớp con. Ngoài ra, lớp con đã định nghĩa lại phương thức class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
04Thứ tự giải quyết phương thức trong Python
Trong Python, Thứ tự phân giải phương thức [MRO] là thứ tự mà Python tìm kiếm một phương thức hoặc thuộc tính. Đầu tiên, phương thức hoặc thuộc tính được tìm kiếm trong một lớp, sau đó nó tuân theo thứ tự mà chúng ta đã chỉ định khi kế thừa
Thứ tự này còn được gọi là Tuyến tính hóa của một lớp và một bộ quy tắc được gọi là MRO [Thứ tự giải quyết phương pháp]. MRO đóng một vai trò thiết yếu trong nhiều kế thừa vì một phương thức duy nhất có thể được tìm thấy trong nhiều lớp cha
Trong đa kế thừa, thứ tự tìm kiếm sau đây được tuân theo
- Đầu tiên, nó tìm kiếm trong lớp cha hiện tại nếu không có, sau đó tìm kiếm trong lớp cha được chỉ định trong khi kế thừa [nghĩa là từ trái sang phải. ]
- Chúng ta có thể lấy MRO của một lớp. Với mục đích này, chúng ta có thể sử dụng thuộc tính
05 hoặc phương thứcclass A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
06class A[A1, A2, A3, ...]: # class A inheriting the properties of A1, A2, A3, etc. # You can add properties to A class too
Thí dụ
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
16Trong ví dụ trên, chúng tôi tạo ba lớp có tên là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
07, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
08 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
09. Lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
08 kế thừa từ class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
07, lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
09 kế thừa từ class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
08 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
07. Khi chúng ta tạo một đối tượng của lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
09 và gọi phương thức class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
16, Python sẽ tìm kiếm phương thức class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
16 trong lớp hiện tại trong chính lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
09Sau đó tìm kiếm các lớp cha, cụ thể là
class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
08 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
07, vì lớp class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
09 kế thừa từ class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
08 và class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
07. nghĩa là, class A[A1, A2, A3, ...]:
# class A inheriting the properties of A1, A2, A3, etc.
# You can add properties to A class too
94 và luôn tìm kiếm theo cách từ trái sang phải