Python 继承 - 一个调用在子类中重写的方法的构造函数,使用哪个方法?

标签 python inheritance

我正在用 python 编写一个类,其中有多个子类:

class Parent:

   def __init__(self, parameters):
      self.MethodA(parameters)

   def MethodA(parameters):
      doStuff

class child1(Parent):

   def MethodA(parameters):
      doOtherStuff

创建child1类型的对象时会用到什么方法?

最佳答案

试试看:

class Parent(object):
    def __init__(self, params):
        self.method(params)

    def method(self, params):
        print "Parent's method called with", params

class Child(Parent):
    def method(self, params):
        print "Child's method called with", params

Child('foo')

输出:

Child's method called with foo

关于Python 继承 - 一个调用在子类中重写的方法的构造函数,使用哪个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4270988/

相关文章:

Python:处理平均时间,第一个/第二个慢得多

c# - 为什么泛型类型参数中的隐式子到父转换是不可能的?

python - 使用 Python 请求修复 SSL 证书错误

python - 将相应列的标题放入 python 中(不使用 pandas)

C++派生类问题

ios - 将子实体追加到核心数据 iOS Swift 中的实体

inheritance - 有没有办法使用 MySQL Workbench 对表继承进行建模?

java - 与基类构造函数相比,什么时候调用 Java 隐式构造函数?

python - 根据查找数据框计算距离

python - 无法让 crypt 正常工作