3.重写父类方法
重写,就是子类中,有一个和父类相同名字的方法,在子类中的方法会覆盖掉父类中同名的方法。
4.调用重名的父类方法
子类重写了父类的方法之后,如何在子类中再调用父类的方法?
方式一:父类名.方法名(self)
方式二:super().方法名()
# @time:2019/10/27 17:03
# @Author:coco
# @File:04重写父类重名的方法.py
# @software:PyCharm
"""
# @time:2019/10/27 17:03
# @Author:coco
# @File:04重写父类重名的方法.py
# @software:PyCharm
"""
重写父类的方法:(父类有一个方法,继承之后,又定义了一个和父类方法名相同的方法)
直接调用重写之后的方法,直接调用自身有的这个方法,不会去父类找
在方法中调用父类被重写的方法:
方式一:父类名.方法名()
方式二:super().方法名()
"""
# 1990
class BasePhone(object):
# 大哥大
def __init__(self, name):
self.name = name
def call_phone(self):
print('打语音电话的功能Base')
# 2005
class Phone_V1(BasePhone):
# 功能机
def call_phone(self):
print('打语音电话的功能V1')
def send_msg(self):
print('发短信')
def music(self):
print('播放音乐')
# 2015
class Phone_V2(Phone_V1):
# 智能机
def call_phone(self):
print('拨打视频电话')
print('时间过去5分钟')
# 在方法中调用父类被重写的方法
# 方式一:类名.方法名(self)
# BasePhone.call_phone(self)
# 方式二:super().方法名()
super().call_phone()
def game(self):
print('玩游戏')
def qq(self):
print('聊qq')
v2 = Phone_V2('华为P30')
v2.call_phone() # 直接调用重写之后的方法,直接调用自身有的这个方法,不会去父类找
运行结果: