【问题标题】:What is the usecase of super() in simple words [duplicate]super() 的简单用例是什么[重复]
【发布时间】:2020-01-31 19:08:40
【问题描述】:

我正在学习 Python,但我发现很难理解 super() 方法在课堂上的作用。有人能用简单的话解释一下它及其用例吗?

【问题讨论】:

  • super 可用于调用任何父类方法,包括init()。
  • 我不喜欢没有任何解释的投票,但我认为原因是因为这个问题可以通过查看语言的文档或在搜索引擎上轻松搜索来轻松回答。跨度>
  • 请避免在脚注中注明社区应该做什么或不应该做什么,投票不是为了冒犯或赞美用户,而只是为了限定帖子,这些是 SO 的规则。除了在实践中指出这种事情之外,他们得到了相反的结果:更多的反对票。
  • 很遗憾,我不喜欢进行广泛的讨论,所以我不会回答任何问题。再见。

标签: python oop super


【解决方案1】:

super 用于调用parent 类的构造函数

At a fairly abstract level, super() provides the access to those methods of the super-class (parent class) which have been overridden in a sub-class (child class) that inherits from it.

参考:https://www.geeksforgeeks.org/python-super-in-single-inheritance/

【讨论】:

  • 子类中的 super().__init__() 是否基本上将父类变量带入子类?
  • 还有,和 Parent.__init__(self) 一样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 2012-04-12
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多