我們沒有爲我們的應用程序類定義一個__init__()方法。在Python中,這就意
味着父方法wx.App.__init()__將在對象創建時被自動調用。這是一個好的事情。如果
你定義你自己的__init__()方法,不要忘了調用其基類的__init()__方法,示例如下:
自動調用父類的__init__方法:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
class A(object):
def __init__(self):
print '----------call A---------------'
self.a = 5
def function_a(self):
print('I am from A, my value is %d' % self.a)
class B(A):
b=122
# print '-------------call B---------------'
# def __init__(self):
# # A.__init__(self) # 此處修改了。如果類A的__init__方法需要傳參,也需要傳入對應的參數
#
# self.b = 10
def function_b(self):
print('I am from B, my value is %d' % self.b)
self.function_a() # 調用類A的方法,出錯
if __name__ == '__main__':
b = B()
b.function_b()
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/wxpython/t6.py
----------call A---------------
I am from B, my value is 122
I am from A, my value is 5