小编dej*_*oma的帖子

在 Python 中调用父类中的子方法

注意:这不是在子类中调用父方法 .super()

我有三个班级,比方说ParentChild1Child2Child1和 2 都有方法Cry()Parent类有另一个方法,例如MakeChildrenStopCry()在其中Cry()被调用。但是,Parent类没有方法Cry()。我需要Cry()Parent类中定义吗?

因为我没有父类的任何对象并且我总是使用子类,所以我只是创建了“空函数”,因为继承只会用Child类中的函数覆盖这些空函数。

def MakeChildrenStopCry(self):
   if self.Cry():
    self.DoWhateverToStopCry(self)
def Cry(self)
   return()
Run Code Online (Sandbox Code Playgroud)

对于完整的示例代码,您可以查看此内容,但我认为上述内容应该很清楚。

这不会在我的代码中造成任何问题,我只是想知道什么是正常完成的,或者以不同的方式设置我的代码是否更好。

python polymorphism inheritance class parent-child

3
推荐指数
1
解决办法
4203
查看次数

标签 统计

class ×1

inheritance ×1

parent-child ×1

polymorphism ×1

python ×1