Python là một ngôn ngữ hướng đối tượng cao. Điều đó có nghĩa là mọi thứ trong Python đều là một đối tượng, giúp việc xây dựng logic OOP bằng Python tương đối dễ dàng
Nếu bạn đang thực hiện Đa kế thừa, bạn nên biết về Thứ tự giải quyết phương thức Python
Trước khi đi sâu vào khái niệm, hãy nhắc nhanh cách viết một lớp có nhiều lớp cha
Để tạo một lớp kế thừa từ nhiều lớp python, chúng ta viết tên của các lớp này bên trong dấu ngoặc đơn cho lớp dẫn xuất khi định nghĩa nó
Chúng tôi phân tách các tên này bằng dấu phẩy
class Animal:
pass
class Bird:
pass
class Duck[Animal, Bird]:
pass
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Hãy giải thích Python MRO ngay bây giờ
Python MRO [Thứ tự giải quyết phương thức]
Một thứ tự được tuân theo khi tìm kiếm một thuộc tính trong một lớp liên quan đến nhiều kế thừa
Đầu tiên, tìm kiếm bắt đầu với lớp hiện tại. Tìm kiếm di chuyển đến các lớp cha từ trái sang phải nếu không tìm thấy
Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về tính đa thừa kế của Python và cách hoạt động của việc phân giải thứ tự phương thức trong Python
Giới thiệu về Python Đa kế thừa
Khi một lớp kế thừa từ một lớp duy nhất, bạn có một kế thừa duy nhất. Python cho phép một lớp kế thừa từ nhiều lớp. Nếu một lớp kế thừa từ hai lớp trở lên, bạn sẽ có nhiều lớp kế thừa
Để mở rộng nhiều lớp, bạn chỉ định các lớp cha bên trong dấu ngoặc đơn
7 sau tên lớp của lớp con như thế nàyCode language: Python [python]
class Car: def go[self]: print['Going']
Code language: Python [python]
class ChildClass[ParentClass1, ParentClass2, ParentClass3]: pass
Cú pháp đa thừa kế tương tự như danh sách tham số trong định nghĩa lớp. Thay vì bao gồm một lớp cha bên trong dấu ngoặc đơn, bạn bao gồm hai hoặc nhiều lớp, được phân tách bằng dấu phẩy
Hãy lấy một ví dụ để hiểu cách hoạt động của đa thừa kế
Đầu tiên, định nghĩa một lớp
8 có phương thứcCode language: Python [python]
class Car: def go[self]: print['Going']
9Code language: Python [python]
class Car: def go[self]: print['Going']
Code language: Python [python]
class Car: def go[self]: print['Going']
Thứ hai, định nghĩa một lớp
0 có phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
1Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Thứ ba, định nghĩa lớp
2 kế thừa từ cả hai lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
8 và lớpCode language: Python [python]
class Car: def go[self]: print['Going']
0Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
class FlyingCar[Flyable, Car]: pass
Vì lớp
2 kế thừa từ lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
8 và lớpCode language: Python [python]
class Car: def go[self]: print['Going']
0 nên nó sử dụng lại các phương thức từ các lớp đó. Điều đó có nghĩa là bạn có thể gọi các phương thức
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
9 vàCode language: Python [python]
class Car: def go[self]: print['Going']
1 trên một thể hiện của lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
2 như thế nàyCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
đầu ra
Code language: Python [python]
Going Flying
Thứ tự giải quyết phương pháp [MRO]
Khi các lớp cha có các phương thức trùng tên và lớp con gọi phương thức đó, Python sẽ sử dụng thứ tự phân giải phương thức [MRO] để tìm kiếm phương thức phù hợp để gọi. Xem xét ví dụ sau
Đầu tiên, thêm phương thức
1 vào các lớpCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
8,Code language: Python [python]
class Car: def go[self]: print['Going']
0 vàCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
2. Trong phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
1 của lớpCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
2, hãy gọi phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
1 của lớpCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
8Code language: Python [python]
class FlyingCar[Flyable, Car]: pass
Code language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
Thứ hai, tạo một thể hiện của lớp
2 và gọi phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
1Code language: Python [python]
class FlyingCar[Flyable, Car]: pass
Code language: Python [python]
if __name__ == '__main__': car = FlyingCar[] car.start[]
đầu ra
Code language: Python [python]
Start the Flyable object
Như bạn có thể thấy rõ ràng từ đầu ra, _
1 gọi phương thức ____12_______1 của lớp _______0Code language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
Sau đây cho thấy lớp
4 của lớpCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
2Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
print[FlyingCar.__mro__]
đầu ra
0Code language: Python [python]
class Car: def go[self]: print['Going']
Từ trái sang phải, bạn sẽ thấy
2,
class Flyable: def fly[self]: print['Flying']
Code language: Python [python]
0,Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
8 vàCode language: Python [python]
class Car: def go[self]: print['Going']
9Code language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
Lưu ý rằng các đối tượng
8 vàCode language: Python [python]
class Car: def go[self]: print['Going']
0 kế thừa hoàn toàn từ lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
9. Khi bạn gọi phương thứcCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
1 từ đối tượng củaCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
2, Python sử dụng đường dẫn tìm kiếm lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
4Code language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
Vì lớp
0 nằm cạnh lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
2, nên lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
1 gọi phương thứcCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
1 của lớpCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
2Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Nếu bạn lật thứ tự của lớp
0 và lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
8 trong danh sách, thì lớpCode language: Python [python]
class Car: def go[self]: print['Going']
4 sẽ thay đổi tương ứng. Ví dụCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
1Code language: Python [python]
class Car: def go[self]: print['Going']
đầu ra
2Code language: Python [python]
class Car: def go[self]: print['Going']
Trong ví dụ này, thay vào đó,
1 gọi phương thứcCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
1 của lớpCode language: Python [python]
class FlyingCar[Flyable, Car]: pass
8, dựa trên thứ tự của chúng trong phân giải thứ tự phương thứcCode language: Python [python]
class Car: def go[self]: print['Going']
Đa thừa kế và siêu
Đầu tiên, thêm phương thức
7 vào lớpCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
8Code language: Python [python]
class Car: def go[self]: print['Going']
3Code language: Python [python]
class Car: def go[self]: print['Going']
Thứ hai, thêm phương thức
7 vào lớp
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
Code language: Python [python]
0Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
4Code language: Python [python]
class Car: def go[self]: print['Going']
Các lớp
7 của lớpCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
8 và lớpCode language: Python [python]
class Car: def go[self]: print['Going']
0 chấp nhận một số lượng tham số khác nhau. Nếu lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
2 kế thừa từ lớpCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
8 và lớpCode language: Python [python]
class Car: def go[self]: print['Going']
0, thì phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
7 của nó cần gọi đúng phương thứcCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
7 được chỉ định trong giải pháp thứ tự phương thứcCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
4 của lớpCode language: Python [python]
if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]
2Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
Thứ ba, thêm phương thức
7 vào lớpCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
2Code language: Python [python]
class Flyable: def fly[self]: print['Flying']
5Code language: Python [python]
class Car: def go[self]: print['Going']
Độ phân giải thứ tự phương thức của lớp
2 làCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
0Code language: Python [python]
class Car: def go[self]: print['Going']
4 gọiCode language: Python [python]
Start the Flyable object
7 của lớpCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
2. Do đó, bạn cần truyền đối sốCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
7 cho phương thứcCode language: Python [python]
Start the Flyable object
7Code language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
Bởi vì lớp
2 không thể truy cập phương thứcCode language: Python [python]
class Flyable: def fly[self]: print['Flying']
7 của lớpCode language: Python [python]
class Car: def start[self]: print['Start the Car'] def go[self]: print['Going'] class Flyable: def start[self]: print['Start the Flyable object'] def fly[self]: print['Flying'] class FlyingCar[Flyable, Car]: def start[self]: super[].start[]
8, bạn cần khởi tạo các thuộc tínhCode language: Python [python]
class Car: def go[self]: print['Going']
2andCode language: Python [python]
print[FlyingCar.__mro__]
3 riêng lẻCode language: Python [python]
print[FlyingCar.__mro__]