这些是我的结果,当调用c2.method1()时,我需要myClass方法1再次返回。 (Python 3)我将如何解决这个问题?我正在使用的教程说调用c2.method1应该导致最后一行" anotherClass方法1"以及" myClass方法1"在第四行结果。
myClass method1
myClass method2: This is a string
anotherClass method1
这是代码。
class myClass():
def method1(self):
print("myClass method1")
def method2(self, someString):
print("myClass method2: " + someString)
class anotherClass(myClass):
def method2(self):
print("anotherClass method2")
def method1(self):
print("anotherClass method1")
def main():
c = myClass()
c.method1()
c.method2("This is a string")
c2 = anotherClass()
c2.method1()
main()
答案 0 :(得分:0)
如果你打电话给' super()'在您的子类初始化(其__init__
)中,您可以调用父类方法:)