0
下面是我的代碼:如何在python中使用子類的方法調用第二個父類的方法?
class Parent1(object):
def __init__(self):
print "!!! ___initialization Parent1___ !!!"
def method(self):
print "*** method of Parent1 is called ***"
class Parent2(object):
def __init__(self):
print "!!! ___initialization Parent2___ !!!"
def method(self):
print "*** method of Parent2 is called ***"
class Child(Parent1,Parent2):
def __init__(self):
print "!!! ___initialization Child___ !!!"
def method(self):
super(Child,self).method()
print "*** method of Child is called ***"
Ch = Child()
Ch.method()
我想打電話給使用子類的對象Parent2
類method()
。條件僅應建立子類對象,並在子類中聲明沒有變化
你也可以將'super(Child,self).method()'改爲'Parent1.method(self)'。這個類層次結構不是爲了正確使用'super'而設計的。 – chepner
@詹姆斯:感謝您的解決方案。這是唯一的方法嗎?有沒有其他方法?不能用super()或decorator來完成嗎? –
@Praveenkumar我並不知道其中之一,但我絕不是Python繼承方面的專家,可能有一些我不知道的方法。 – James