【发布时间】:2014-11-20 04:10:53
【问题描述】:
标题几乎是不言自明的,但我认为这最好用一个例子来解释。
class Dog():
def __init__(self, name):
self.name = name
def get_name(self):
return self.name
def get_color(self):
return body_color()
class personality_1():
def get_happiness(self):
return happiness_with_owner()
def get_sadness(self):
return sadness()
## A lot more personality methods here
class SocialDog(Dog):
# Override regular method
def get_color(self):
return face_color()
# I want to override the personality 1 class but not completely, just one method
class personality_2(>>>How to inherit from personality_1?<<<):
# Now, I would like to override just one method of personality 1:
def get_happiness(self):
return happiness_with_others()
希望逻辑是正确的。我试图使用 super() 没有成功。希望我可以在不显式调用父类的情况下找到解决方案。
有什么想法吗?
提前致谢!
【问题讨论】:
标签: python class inheritance subclass