基本上,我的問題是問here並有一些答案,像下面,其中Son
確實喜歡Father
一些東西(在這種情況下,INIT)和一些其他的東西像GrandFather
(do_thing
)。調用父母的方法,有什麼後果?
class GrandFather(object):
def __init__(self):
pass
def do_thing(self):
# stuff
class Father(GrandFather):
def __init__(self):
super(Father, self).__init__()
def do_thing(self):
# stuff different than Grandfather stuff
class Son(Father):
def __init__(self):
super(Son, self).__init__()
def do_thing(self):
super(Father, self).do_thing() # <-- this line bypasses Father's implementation
我想知道是否有調用super
像(最後一行以上),即,通過比你自己以外的類類型的任何後果。 我的意思是像你的代碼打破在你不期望它的一些奇怪的點。
什麼是尚未涵蓋[這裏](http://stackoverflow.com/questions/5033903/python-super-method-and-calling-alternatives),[這裏](http://stackoverflow.com/questions/222877 /什麼是超級在做蟒蛇)或[這裏](http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods)? –
@StephenRauch什麼不包括在內?我的問題沒有涵蓋! :D注意,在我的例子中,最後一行是'super(Father,self)'而不是'super(Son,self)'。後者是使用'super'的正常方式,這在您提供的鏈接中已經介紹過了。然而,我的問題是關於前者,我忽略了直接父類,並在祖父類中調用該方法! – Mahdi