Software Design 2010年6月号《補足》#1: 図解(総天然色版)

小粒ちゃんねる
小粒ちゃんねる #小粒ch

《著》小粒ちゃん@湘南組、小粒ちゃん@博多組

Software Design 連載記事

ここでは、掲載記事に関する補足情報を公開しています。

老頭児(ロートル)エンジニアのつぶやき

大域変数と鋏は使いよう

  • 図1 実行中のアプリケーション:メソッド変更前



  • 図2 関数オブジェクトを利用して、メソッドを再定義する


  • 図3 実行中のアプリケーション:メソッド変更後


class Tips(JPanel): 
    def __init__(self, *args, **keys): 
        comp = JButton( 
            text = "Click Me", 
            preferredSize = (200, 50), 
            actionPerformed = self.actionPerformed, 
        ) 
        self.add(comp) 
    def actionPerformed(self, e): 
        comp = e.source 
        comp.text = "%s"%ctime() 

def tips(): 
    frame = JFrame( 
        title = "Timer", 
        defaultCloseOperation = JFrame.EXIT_ON_CLOSE, 
    ) 
    global Qte; Qte = \ 
    comp = Tips() 
    frame.contentPane = comp 
    frame.visible = True 
《余録》
  • リスト1:別解

記事では、Java と比較しやすいコードにしましたが、Jython の特徴を活かすと、こんな感じに…

class Tips(JPanel): 
    def __init__(self, *args, **keys): 
        comp = JButton( 
            text = "Click Me", 
            preferredSize = (200, 50), 
            actionPerformed = self, 
        ) 
        self.add(comp) 
    def __call__(self, e): 
        comp = e.source 
        comp.text = "%s"%ctime() 

def tips(): 
    ... 

TOP


関連記事

  • @

Last updated♪2010/05/16