python构造函数 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.4\python.exe E:/python/pythonfile20190907/OOP6.py
父类构造函数
子类构造函数
孙类构造函数
Process finished with exit code 0
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。