• python 多重继承构造函数调用顺序


    • 实例代码
      class Person(object):
          def __init__(self, name, age):
              self.name = name
              self.age = age
              print("父类构造函数")
      
      
          def talk(self):
              print("person is talking....")
      
      
      class Chinese(Person):
          def __init__(self, name, age, language):  # 先继承,在重构
              Person.__init__(self, name, age)  # 继承父类的构造方法,也可以写成:super(Chinese,self).__init__(name,age)
              self.language = language  # 定义类的本身属性
              print("子类构造函数")
      
          def walk(self):
              print('is walking...')
      
      
      class American(Chinese):
          def __init__(self, name, age, language,height):
              Chinese.__init__(self,name,age,language)
              self.height=height
              print("孙类构造函数")
      
      
      c = American('bigberg', 22, 'Chinese',180)
    • 运行结果
      D:软件安装Python3.7.4python.exe E:/python/pythonfile20190907/OOP6.py
      父类构造函数
      子类构造函数
      孙类构造函数
      
      Process finished with exit code 0
  • 相关阅读:
    leetcode 1 Two sum
    hdu1099
    hdu1098
    函数执行顺序
    浏览器滚动条
    2048的制作
    JavaScript--对象-检查一个对象是否是数组
    JavaScript--格式化当前时间
    JavaScript--模拟验证码
    JavaScript--模拟网络爬虫
  • 原文地址:https://www.cnblogs.com/ybl20000418/p/11491903.html
Copyright © 2020-2023  润新知