背景:世间万物总是有着这样或者那样的联系:譬如
为了解决上诉问题:就引入了多重继承
譬如在漫威宇宙里面:
class Marvel_Universe(object):#老祖宗类
pass
class Earthhero(Marvel_Universe):#大类
pass
class OtherStarhero(Marvel_Universe):#大类
pass
class Technology_class(Earthhero):
pass
class Superpower_class_On_E(Earthhero):
pass
class Superpower_class_On_other(OtherStarhero)
pass
class Tony_stark(Technology_class): #那么我们的Iron—man就相当于继承Technology_class,Earthhero,Marvel_Universe 三类
def __init__(self,name,power,age,wealth,equipment):
self.name=name
self.power=power
self.age=age
self.wealth=wealth
self.equipment=equipment
def IDprint(self):
print('I am Iron man')
关于__init__()方法的一些继承,基于上一个Tony_stark,我们写一个morgen_stark类
class Morgen_stark(Tony_stark):
def __init__(self, age, honey_value):
Tony_stark.__init__(self,'stark', 1000, 45, 1e100, 'a helmet and armor') # 继承了tony的一些属性
self.age = age # 小摩根自己的属性
self.honey_value = honey_value
def getMorgeninfo(self):
print('能力', self.power,
'财富', self.wealth,
'装备', self.equipment,
'年龄', self.age,
'可爱值', self.honey_value)
Iron_man=Tony_stark('stark',1000,45,1e100,'a helmet and armor')
Iron_girl= Morgen_stark(8,3000)
Iron_girl.getMorgeninfo()
#>>能力 1000 财富 1e+100 装备 a helmet and armor 年龄 8 可爱值 3000
关于super()方法的操作 ,我们还是使用到Tony_stark这个类
class Morgen_stark(Tony_stark):
def __init__(self, age, honey_value):
Tony_stark.__init__(self,'stark', 1000, 45, 1e100, 'a helmet and armor') # 继承了tony的一些属性
def Info(self):
print('my father`s pet phrase is ')
super().IDprint()#super()函数的作用
Iron_man=Tony_stark('stark',1000,45,1e100,'a helmet and armor')
Iron_girl= Morgen_stark(8,3000)
Iron_girl.getMorgeninfo()