Thứ tự đa kế thừa trong Python

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

class Car: def go[self]: print['Going']

Code language: Python [python]
7 sau tên lớp của lớp con như thế này

class ChildClass[ParentClass1, ParentClass2, ParentClass3]: pass

Code language: Python [python]

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

class Car: def go[self]: print['Going']

Code language: Python [python]
8 có phương thức

class Car: def go[self]: print['Going']

Code language: Python [python]
9

class Car: def go[self]: print['Going']

Code language: Python [python]

Thứ hai, định nghĩa một lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 có phương thức

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
1

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]

Thứ ba, định nghĩa lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 kế thừa từ cả hai lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]

Vì lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 kế thừa từ lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
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 Car: def go[self]: print['Going']

Code language: Python [python]
9 và

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
1 trên một thể hiện của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 như thế này

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]

đầu ra

Going Flying

Code language: Python [python]

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

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 vào các lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8,

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 và

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2. Trong phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2, hãy gọi phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 của lớp

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
8

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]

Thứ hai, tạo một thể hiện của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 và gọi phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1

if __name__ == '__main__': car = FlyingCar[] car.start[]

Code language: Python [python]

đầu ra

Start the Flyable object

Code language: Python [python]

Như bạn có thể thấy rõ ràng từ đầu ra, _

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
1 gọi phương thức ____12_______1 của lớp _______0

Sau đây cho thấy lớp

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
4 của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2

print[FlyingCar.__mro__]

Code language: Python [python]

đầu ra

class Car: def go[self]: print['Going']

Code language: Python [python]
0

Từ trái sang phải, bạn sẽ thấy

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2,

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0,

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
9

Lưu ý rằng các đối tượng

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 kế thừa hoàn toàn từ lớp

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
9. Khi bạn gọi phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 từ đối tượng của

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2, Python sử dụng đường dẫn tìm kiếm lớp

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
4

Vì lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 nằm cạnh lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2, nên lớp

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
1 gọi phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2

Nếu bạn lật thứ tự của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 và lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8 trong danh sách, thì lớp

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
4 sẽ thay đổi tương ứng. Ví dụ

class Car: def go[self]: print['Going']

Code language: Python [python]
1

đầu ra

class Car: def go[self]: print['Going']

Code language: Python [python]
2

Trong ví dụ này, thay vào đó,

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
1 gọi phương thức

class FlyingCar[Flyable, Car]: pass

Code language: Python [python]
1 của lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8, dựa trên thứ tự của chúng trong phân giải thứ tự phương thức

Đa thừa kế và siêu

Đầu tiên, thêm phương thức

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]
7 vào lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8

class Car: def go[self]: print['Going']

Code language: Python [python]
3

Thứ hai, thêm phương thức

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]
7 vào lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0

class Car: def go[self]: print['Going']

Code language: Python [python]
4

Các 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]
7 của lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0 chấp nhận một số lượng tham số khác nhau. Nếu lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 kế thừa từ lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8 và lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
0, thì phương thức

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]
7 của nó cần gọi đúng phương thức

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]
7 được chỉ định trong giải pháp thứ tự phương thức

if __name__ == '__main__': fc = FlyingCar[] fc.go[] fc.fly[]

Code language: Python [python]
4 của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2

Thứ ba, thêm phương thức

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]
7 vào lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2

class Car: def go[self]: print['Going']

Code language: Python [python]
5

Độ phân giải thứ tự phương thức của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 là

class Car: def go[self]: print['Going']

Code language: Python [python]
0

Start the Flyable object

Code language: Python [python]
4 gọi

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]
7 của lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2. Do đó, bạn cần truyền đối số

Start the Flyable object

Code language: Python [python]
7 cho phương thức

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]
7

Bởi vì lớp

class Flyable: def fly[self]: print['Flying']

Code language: Python [python]
2 không thể truy cập phương thức

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]
7 của lớp

class Car: def go[self]: print['Going']

Code language: Python [python]
8, bạn cần khởi tạo các thuộc tính

print[FlyingCar.__mro__]

Code language: Python [python]
2and

print[FlyingCar.__mro__]

Code language: Python [python]
3 riêng lẻ

Thứ tự có quan trọng trong nhiều kế thừa trong Python không?

Có, bạn có thể thực hiện nhiều kế thừa . vui lòng lưu ý thứ tự của lớp trong các vấn đề của ExampleSimMod.

Làm thế nào có thể có nhiều kế thừa trong Python?

Một lớp có thể được bắt nguồn từ nhiều hơn một lớp cơ sở trong Python , tương tự như C++. Cái này gọi là đa thừa kế. Trong đa kế thừa, các tính năng của tất cả các lớp cơ sở được kế thừa vào lớp dẫn xuất. Cú pháp của đa thừa kế tương tự như đơn thừa kế.

Phương pháp MRO trong Python là gì?

Thứ tự giải quyết phương thức [MRO] nó biểu thị cách ngôn ngữ lập trình giải quyết một phương thức hoặc thuộc tính . Python hỗ trợ các lớp kế thừa từ các lớp khác. Lớp được kế thừa được gọi là Lớp cha hoặc Lớp cha, trong khi lớp kế thừa được gọi là Lớp con hoặc Lớp con.

Chủ Đề