-----------支持作者请转发本文-----------李宁老师已经在「极客起源」 微信公众号推出《Python编程思想》电子书,囊括了Python的核心技术,以及Python的主要函数库的使用方法。读者可以在「极客起源」 公众号中输入 160442 开始学习。-----------正文-----------继承是面向对象的3大特征之一(另两个特性是封装和组合),也是实现软件复用的重要手段。Python的继承是多继承机制,也就是一个子类可以同时有多个直接父类。1.  继承的语法Python子类继承父类的语法是在定义子类时,将多个父类放在子类之后的圆括号中。语法格式如下:
 
class SubClass(SuperClassl, SuperClass2,..)     # 类定义部分class Fruit:    def info(self):        print(f"我是水果!重{self.weight}克" )class Food:    def taste(self):        print("不同食物的口感不同")# 定义Banana类,继承了Fruit和Food类class Banana(Fruit, Food):    pass# 创建Banana对象b = Banana()b.weight = 16# 调用Banana对象的info()方法b.info()# 调用Banana对象的taste()方法b.taste()class Item:    def info (self):        print("Item中方法:", '这是一个商品') class Product:    def info (self):        print("Product中方法:", '这是一个移动产品')class Mouse1(Item, Product):    passclass Mouse2(Product, Item):    passm1 = Mouse1()m1.info()m2 = Mouse2()m2.info()class Bird:     # Bird类的fly()方法    def fly(self):        print("我在天空里自由自在地飞翔...")class Ostrich(Bird):    # 重写Bird类的fly()方法    def fly(self):        print("我只能在地上奔跑...")  # 创建Ostrich对象os = Ostrich()# 执行Ostrich对象的fly()方法,将输出"我只能在地上奔跑..."os.fly()class BaseClass:    def name (self):        print('父类中定义的name方法')class SubClass(BaseClass):    # 重写父类的name方法    def name (self):        print('子类重写父类中的name方法')    def process (self):        print('执行process方法')        # 直接执行name方法,将会调用子类重写之后的name()方法        self.name()        # 使用类名调用实例方法调用父类被重写的方法        BaseClass.name(self)sc = SubClass()sc.process()class Employee :    def __init__ (self, salary):        self.salary = salary    def work (self):        print('普通员工正在写代码,工资是:', self.salary)class Customer:    def __init__ (self, favorite, address):        self.favorite = favorite        self.address = address    def info (self):        print(f'我是一个顾客,我的爱好是: {self.favorite},地址是{self.address}' )class Manager1 (Employee,Customer):    passclass Manager2 (Customer, Employee):    passm1 = Manager1(1235)m1.work()m2 = Manager2('服务器', '北京')m2.info()- 使用未绑定方法,这种方式很容易理解。因为构造方法也是实例方法。当然可以通过这种方式来调用; 
- 使用supe()函数调用父类的构造方法; 
class super(object)   super() -> same as super(__class__, )   super(type) -> unbound super object   super(type, obj) -> bound super object; requires isinstance(obj, type)   super(type, type2) -> bound super object; requires issubclass(type2, type)   Typical use to call a cooperative superclass method:   class C(B):       def meth(self, arg):           super().meth(arg)   This works for class methods too:   class C(B):       @classmethod       def cmeth(cls, arg):           super().cmeth(arg)      Methods defined here:      __get__(self, instance, owner, /)       Return an attribute of instance, which is of type owner.      __getattribute__(self, name, /)class Employee :    def __init__ (self, salary):        self.salary = salary    def work (self):        print('普通员工正在写代码,工资是:', self.salary)class Customer:    def __init__ (self, favorite, address):        self.favorite = favorite        self.address = address    def info (self):        print(f'我是一个顾客,我的爱好是: {self.favorite},地址是{self.address}')# Manager继承了Employee、Customerclass Manager(Employee, Customer):    # 重写父类的构造方法    def __init__(self, salary, favorite, address):        print('--Manager的构造方法--')        # 通过super()函数调用父类的构造方法        super().__init__(salary)        # 使用未绑定方法调用父类的构造方法        Customer.__init__(self, favorite, address)# 创建Manager对象m = Manager(25000, 'IT产品', '广州')m.work()m.info()--Manager的构造方法--普通员工正在写代码,工资是: 25000我是一个顾客,我的爱好是: IT产品,地址是广州
