继承: 基于通用类创造出专用类
我们可以继承已有类的属性,生成新的类。

单继承
┌───────────────┐│ object │└───────────────┘│┌────────────┴────────────┐│ │▼ ▼┌─────────────┐ ┌─────────────┐│ Animal │ │ Plant │└─────────────┘ └─────────────┘│ │┌─────┴──────┐ ┌─────┴──────┐│ │ │ │▼ ▼ ▼ ▼┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐│ Dog │ │ Cat │ │ Tree │ │ Flower │└─────────┘ └─────────┘ └─────────┘ └─────────┘
class Animal(object):def run(self):print('Animal is runnung')class Dog(Animal):def run(self):print("Dog is running...")def eat(self):print("Eating meat...")class Cat(Animal):passdog=Dog()dog.run()dog.eat()
MRO
在继承多个类时,可能会遇到不同类中有相同方法的情况,这时Pythin会使用MRO顺序来搜索不同类之间的方法,大致为深度优先、从左至右,并确保每个基类至多访问一次。
class A():passclass B():passclass C(A,B):pass>>> C.__mro__(__main__.C, __main__.A, __main__.B, object)
https://docs.python.org/zh-cn/3/tutorial/classes.html#multiple-inheritance
