Python 如何使用super()从多个父类继承特定类?
我的代码是这样的,我想用super()来继承Papa的特性,怎么做Python 如何使用super()从多个父类继承特定类?,python,inheritance,multiple-inheritance,super,Python,Inheritance,Multiple Inheritance,Super,我的代码是这样的,我想用super()来继承Papa的特性,怎么做 class Mama(object): def __init__(self): self.name = 'Mama' def feature(self): print "%s have big eyes" % self.name class Papa(object): def __init__(self): self.name = 'Papa'
class Mama(object):
def __init__(self):
self.name = 'Mama'
def feature(self):
print "%s have big eyes" % self.name
class Papa(object):
def __init__(self):
self.name = 'Papa'
def feature(self):
print "%s have fierce beards" % self.name
class Offspring(Mama,Papa):
def __init__(self, name):
self.name = name
def feature(self):
super(Offspring, self).feature()
offspring = Offspring('Tommy')
offspring.feature()
# This will result "Tommy have big eyes"
您可以先从Papa
继承来更改MRO(方法解析顺序):
另一种方法是跳过MRO并显式调用Papa
上的(未绑定)方法:
class Offspring(Mama, Papa):
def __init__(self, name):
self.name = name
def feature(self):
Papa.feature(self)
您可以先从Papa
继承来更改MRO(方法解析顺序):
另一种方法是跳过MRO并显式调用Papa
上的(未绑定)方法:
class Offspring(Mama, Papa):
def __init__(self, name):
self.name = name
def feature(self):
Papa.feature(self)
您的继承人中的所有类都需要使用
super
,才能通过所有方法。最终,您将遇到一个问题,即下一个超类是对象
,它没有功能
,因此您还需要检测该情况并忽略它-即,您需要执行以下操作:
class Mama(object):
def __init__(self):
self.name = 'Mama'
def feature(self):
try:
super(Mama, self).feature()
except AttributeError:
# only superclass is object
pass
print "%s have big eyes" % self.name
class Papa(object):
def __init__(self):
self.name = 'Papa'
def feature(self):
try:
super(Papa, self).feature()
except AttributeError:
# only superclass is object
pass
print "%s have fierce beards" % self.name
class Offspring(Mama,Papa):
def __init__(self, name):
self.name = name
def feature(self):
super(Offspring, self).feature()
除了捕获AttributeError之外,您还可以创建一个仅为提供功能
(而不调用超级
)以供其他类继承的其他类。然后,Mama和Papa都继承该类并覆盖功能
,如下所示:
class Grandma(object):
def feature(self):
pass
class Mama(Grandma):
def feature(self):
super(Mama, self).feature()
print "%s have big eyes" % self.name
您可能想考虑制作<代码>特性an,强调它只存在于继承。< /P>
在任何一种情况下,都会发生这样的情况:您将继续调用下一个方法,直到到达链的末尾。如果
Mama
和Papa
都不调用super,那么在一次调用之后,您将始终停止调用。您继承人中的所有类都需要使用super
,以便它通过所有方法。最终,您将遇到一个问题,即下一个超类是对象
,它没有功能
,因此您还需要检测该情况并忽略它-即,您需要执行以下操作:
class Mama(object):
def __init__(self):
self.name = 'Mama'
def feature(self):
try:
super(Mama, self).feature()
except AttributeError:
# only superclass is object
pass
print "%s have big eyes" % self.name
class Papa(object):
def __init__(self):
self.name = 'Papa'
def feature(self):
try:
super(Papa, self).feature()
except AttributeError:
# only superclass is object
pass
print "%s have fierce beards" % self.name
class Offspring(Mama,Papa):
def __init__(self, name):
self.name = name
def feature(self):
super(Offspring, self).feature()
除了捕获AttributeError之外,您还可以创建一个仅为提供功能
(而不调用超级
)以供其他类继承的其他类。然后,Mama和Papa都继承该类并覆盖功能
,如下所示:
class Grandma(object):
def feature(self):
pass
class Mama(Grandma):
def feature(self):
super(Mama, self).feature()
print "%s have big eyes" % self.name
您可能想考虑制作<代码>特性an,强调它只存在于继承。< /P>
在任何一种情况下,都会发生这样的情况:您将继续调用下一个方法,直到到达链的末尾。如果
Mama
和Papa
都不叫super,你总是会在一个电话后停下来。是你又比我快了+1我认为第二种方法可以很好地解决我的问题。是你又比我快了+1我认为第二种方法可以很好地解决我的问题。谢谢。我只是想知道如何得到一个理想的父亲类的方法。今天下午我做了一点调查。如果有多级父类,则使用相同的方法。只是super(target\u父类,self)。method()将调用该父类的父方法。谢谢。我只是想知道如何得到一个理想的父亲类的方法。今天下午我做了一点调查。如果有多级父类,则使用相同的方法。只是super(target\u father\u class,self)。method()将调用该father\u类的父方法。