一. 子类继承父类的构造函数
现金盘平台盘口搭建q<277.03.4.83.6>
子类不重写 init,实例化子类时,会自动调用父类定义的 init。
#-*-coding:utf-8-*-
class Father(object):
def __init__(self, first_name,last_name):
self.first_name = first_name
self.last_name = last_name
print ( "name: %s%s" %( self.first_name,self.last_name) )
def getName(self):
return 'Father ' + self.name
class Son(Father):
def getName(self):
return 'Son '+self.first_name
if __name__=='__main__':
son = Son('li','si')
print ( son.getName() )
输出结果为:
name: lisi
Son li
2 . 重写init ,实例化子类,就不会调用父类已经定义的 init
#-*-coding:utf-8-*-
class Father(object):
def __init__(self, first_name,last_name):
self.first_name = first_name
self.last_name = last_name
print ( "name: %s%s" %( self.first_name,self.last_name) )
def getName(self):
return 'Father ' + self.name
class Son(Father):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
print("name: %s" %( self.last_name))
def getName(self):
return 'Son '+self.last_name
if __name__=='__main__':
son = Son('li','si')
print ( son.getName() )