赞
踩
一、单继承
- 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):
- pass
-
- if __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()

赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。