【发布时间】:2020-04-22 04:39:42
【问题描述】:
希望你们一切都好。我需要做一些功课,我提出了一个疑问,我不知道是否可以通过这种方式解决。如果你能帮助我,你介意吗?还是谢谢。
假设我们有这个:
class human:
def dress(self, weather):
return color(weather)
class girl(human):
def color(self, weather):
if weather == 'rainy':
print('red')
else:
print('blue')
我可以这样做吗?因为乍一看,我有一个问题,即人类的 color() 是未定义的(逻辑上)。你可能会问,你为什么会这样想?因为是解决问题的说明。哪个最好解决?
再次感谢!
【问题讨论】:
-
color在基类中调用它时可能应该是self.color。在这种情况下,这不是问题。实际上,请查看所谓的“模板方法模式”,这正是需要的。也就是说,作为新用户,请使用tour 并阅读How to Ask。
标签: python class object inheritance method-resolution-order