class A(object):
def routine(self):
print "A.routine()"
class B(A):
def routine(self):
print "B.routine()"
A().routine()
def fun():
b = B()
b.routine()
if __name__ == '__main__':fun()
当我使用上面的代码时,A()。例程在类A的方法中执行命令但是当我使用代码时:
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
wx.Frame().__init__(parent, title=title, size=(300, 200))
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
Example(None, title='Size')
app.MainLoop()
为什么会这样呢
wx.Frame().__init__(parent, title=title, size=(300, 200))
与
类似A().routine()
而是显示错误: TypeError:必需参数' parent' {pos 1}未找到
答案 0 :(得分:0)
代码
A().routine()
创建一个 new A
对象,并在那个对象上调用该方法。
要为您自己的对象调用基类方法,请使用:
super(B, self).routine()
对于您的示例框架,请使用:
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title, size=(300, 200))
...
如果您确实不想使用super
,请明确调用基类方法:
class Example(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(300, 200))
...