【发布时间】:2021-12-12 09:28:30
【问题描述】:
我的目标:在调用 Parent2 类的 [identify] 方法的子类中创建名为 [identify2] 的方法。这必须使用 super() 关键字来完成。
我的问题:两个父类中的方法名称相同。但我只想涉及使用 super() 关键字的 parent2 类的方法。我该怎么办?
class Parent1:
def identify(self):
return "This method is called from Parent1"
class Parent2:
def identify(self):
return "This method is called from Parent2"
# declare child class here
class child(Parent1, Parent2):
def identify(self):
return "This method is called from Child"
def identify2(self):
super().identify() # I want to call the method of Parent2 class, how?
child_object = child()
# Expected output:
print( child_object.identify() ) # This method is called from Child
print( child_object.identify2() ) # This method is called from Parent2
【问题讨论】:
标签: python oop inheritance overriding