• 实例代码
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.4\python.exe E:/python/pythonfile20190907/OOP6.py
父类构造函数
子类构造函数
孙类构造函数

Process finished with exit code 0