一个公用的改进了的按钮栏方法-lc滤波器和螺旋滤波器的设计

例5.2按钮栏作为一个单独的方法def createButtonBar(self):     firstButton = wx.Button(panel, -1, ”FIRST”)     self.Bind(wx.EVT_BUTTON, self.OnFirst, firstButton)     prevButton = wx.Button(panel, -1, ””, pos=(160, 0))     self.Bind(wx.EVT_BUTTON, self.OnNext, nextButton)     lastButton = wx.Button(panel, -1, ”LAST”, pos=(240, 0))     self.Bind(wx.EVT_BUTTON, self.OnLast, lastButton)向上面这样把代码分离出后,所有按钮添加代码之间的共性就很容易看出来了。我们可以把添加按钮的代码写成一个公用的方法来调用,而避免了重复。如例5.3所示:例5.3一个公用的改进了的按钮栏方法def createButtonBar(self, panel):     self.buildOneButton(panel, ”First”, self.OnFirst)     self.buildOneButton(panel, ””, self.OnNext, (160, 0))     self.buildOneButton(panel, ”Last”, self.OnLast, (240, 0)) def buildOneButton(self, parent, label, handler, pos=(0,0)):     button = wx.Button(parent, -1, label, pos)     self.Bind(wx.EVT_BUTTON, handler, button)     return button例5.3代替例5.2有两个好处。第一,简短的方法和有意义的方法名使得代码的可读性更清晰了。第二,它避免了局部变量(诚然,你也可以通过使用ID来避免使用局部变量,但那容易导致重复的ID问题)。不使用局部变量是有好113 / 565
pdf 文件大小:3.72MB