Python__子类调用父类的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python__子类调用父类的方法相关的知识,希望对你有一定的参考价值。
# class OldboyPeople:
# school = ‘oldboy‘
# def __init__(self,name,age,sex):
# self.name=name
# self.age=age
# self.sex=sex
#
# def eat(self):
# print(‘is eating‘)
# def teach(self):
# print(‘这是父类的teach‘)
#
# class OldboyTeacher(OldboyPeople):
# def __init__(self,name,age,sex,salary,title):
# # OldboyPeople.__init__(self,name,age,sex)
# #在Python2中需要写全:super(OldboyTeacher,self)
# super().__init__(name,age,sex)
# self.salary=salary
# self.title=title
#
# def teach(self):
# # OldboyPeople.teach(self)
# super().teach()
# print(‘%s is teaching‘ %self.name)
# print(OldboyTeacher.mro())
#
# egon_obj=OldboyTeacher(‘egon‘,18,‘male‘,3.1,‘沙河霸道金牌讲师‘)
# # print(egon_obj.title)
# # print(egon_obj.__dict__)
# egon_obj.teach()
class A:
def test(self):
super().test()
class B:
def test(self):
print(‘B‘)
class C(A,B):
pass
# a=A()
# a.test()
print(C.mro())
c=C()
c.test()
以上是关于Python__子类调用父类的方法的主要内容,如果未能解决你的问题,请参考以下文章
Python3基础 super层层调用父类的__init__方法 子类的__init__覆盖了父类的__init__的解决方法