调用父类属性方法
代码1.1:
class Father():
def __init__(self):
self.a='aaa'
def action(self):
print('调用父类的方法')
class Son(Father):
pass
son=Son() # 子类Son 继承父类Father的所有属性和方法
son.action() # 调用父类方法
son.a # 调用父类属性
重写父类属性方法
注意:在上面的例子中,子类Son没有属性和action的方法,所以会从父类调用,那我们再来看看,子类Son有自身的属性和方法的结果是怎样的?
上述代码修改为:
class Father():
def __init__(self):
self.a='aaa'
def action(self):
print('调用父类的方法')
class Son(Father):
def __init__(self):
self.a='bbb'
def action(self):
print('子类重写父类的方法')
son=Son() # 子类Son继承父类Father的所有属性和方法
son.action() # 子类Son调用自身的action方法而不是父类的action方法
son.a



