您的位置:首页 > 文旅 > 旅游 > 面向对象——继承(python)

面向对象——继承(python)

2025/1/10 9:04:27 来源:https://blog.csdn.net/weixin_47347921/article/details/140831036  浏览:    关键词:面向对象——继承(python)

一、单继承

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master):pass
if __name__ == '__main__':result = Prentice()result.make_cake()print(Prentice.__mro__)

二、多继承

多继承就是一个类同事继承了多个父类

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class School():def __init__(self):self.kongfu = "[香辣煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master):pass
if __name__ == '__main__':result = Prentice()result.make_cake()print(Prentice.__mro__)

注意:当一个类有多个父类的时候。默认使用第一个父类的同名属性和方法

三、子类重写父类的同名方法和属性

子类和父类具有同名属性和方法,默认使用子类的同名属性和方法。

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class School():def __init__(self):self.kongfu = "[香辣煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master):def __init__(self):self.kongfu = "[独创煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")pass
if __name__ == '__main__':result = Prentice()result.make_cake()print(Prentice.__mro__)

四、子类调用父类的同名方法和属性

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class School():def __init__(self):self.kongfu = "[香辣煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master,School):def __init__(self):self.kongfu = "[独创煎饼果子配方]"def make_cake(self):self.__init__()print(f"运用{self.kongfu}")def make_master_cake(self):Master.__init__(self)Master.make_cake(self)def make_school_cake(self):School.__init__(self)School.make_cake(self)if __name__ == '__main__':result = Prentice()result.make_cake()print(Prentice.__mro__)result.make_master_cake()result.make_school_cake()

五、多层继承

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class School():def __init__(self):self.kongfu = "[香辣煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master,School):def __init__(self):self.kongfu = "[独创煎饼果子配方]"def make_cake(self):self.__init__()print(f"运用{self.kongfu}")def make_master_cake(self):Master.__init__(self)Master.make_cake(self)def make_school_cake(self):School.__init__(self)School.make_cake(self)class Tusun(Prentice):passif __name__ == '__main__':result = Tusun()result.make_cake()print(Tusun.__mro__)result.make_master_cake()result.make_school_cake()

六、super()调用父类方法

使用super()可以自动查找父类。调用顺序遵循__mro__类属性的顺序。比较适合单继承实用。

class Master():def __init__(self):self.kongfu = "[五香煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class School():def __init__(self):self.kongfu = "[香辣煎饼果子配方]"def make_cake(self):print(f"运用{self.kongfu}")
class Prentice(Master,School):def __init__(self):self.kongfu = "[独创煎饼果子配方]"def make_cake(self):self.__init__()print(f"运用{self.kongfu}")def make_master_cake(self):Master.__init__(self)Master.make_cake(self)def make_school_cake(self):School.__init__(self)School.make_cake(self)def make_old_cake(self):#     # Master.__init__(self)#     # Master.make_cake(self)#     # School.__init__(self)#     # School.make_cake(self)super(Prentice,self).__init__()super(Prentice,self).make_cake()class Tusun(Prentice):def make_old_cake(self):# Master.__init__(self)# Master.make_cake(self)# School.__init__(self)# School.make_cake(self)super().__init__()super().make_cake()if __name__ == '__main__':result = Tusun()r = Prentice()# result.make_cake()# print(Tusun.__mro__)# result.make_master_cake()# result.make_school_cake()result.make_old_cake()r.make_old_cake()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com