重用父类功能的两种方式
# 在子派生的新方法中重用父类功能的两种方式
# 方式一:与继承无关
#指名道姓法,直接用:类名.函数名
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# class OldboyStudent(OldboyPeople):
# def __init__(self,name,age,sex,stu_id):
# OldboyPeople.__init__(self,name,age,sex)
# self.stu_id=stu_id
#
# def choose_course(self):
# print('%s is choosing course' %self.name)
# 方式二:严格依赖继承属性查找关系
# super()会得到一个特殊的对象,该对象就是专门用来访问父类中的属性的(按照继承的关系)
# super().__init__(不用为self传值)
# 注意:
# super的完整用法是super(自己的类名,self),在python2中需要写完整,而python3中可以简写为super()
# class OldboyPeople:
# school = 'oldboy'
#
# def __init__(self, name, age, sex):
# self.name = name
# self.age = age
# self.sex = sex
#
# class OldboyStudent(OldboyPeople):
# def __init__(self,name,age,sex,stu_id):
# # OldboyPeople.__init__(self,name,age,sex)
# super(OldboyStudent,self).__init__(name,age,sex)
# self.stu_id=stu_id
#
# def choose_course(self):
# print('%s is choosing course' %self.name)
#
#
# stu1=OldboyStudent('猪哥',19,'male',1)
# print(stu1.__dict__)
#
# print(OldboyStudent.mro())
class A:
def f1(self):
print('A.f1')
class B:
def f1(self):
print("hahaha")
def f2(self):
super(B,self).f1()
print('B.f2')
class C(B,A):
def f1(self):
print("wawa")
obj=C() # 生成一个C的对象
print(C.mro()) #C-》B->A->object
obj.f2() # 寻找f2函数属性 先在obj找,没有到obj的类C中找,没有到父类B找(从左到右),找到了f2,
# 执行f2,遇到super().f1(),在父类中找f1,B没有父类。此时光标在B处,按照mro列表继续往下找
# [<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
越是困难的事越要立即去做,这样收益才会最大!!!