我正在用 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/