posts - 431,  comments - 344,  trackbacks - 0
          Python似乎很討厭修飾符,沒有常見的static語法。其靜態(tài)方法的實(shí)現(xiàn)大致有以下兩種方法:

          第一種方式(staticmethod):

          >>> class Foo:
                  str = "I'm a static method."

                  def bar():
                      print Foo.str

                  bar = staticmethod(bar)


          >>> Foo.bar()
          I'm a static method.


          第二種方式(classmethod):

          >>> class Foo:
                  str = "I'm a static method."

                  def bar(cls):
                      print cls.str

                  bar = classmethod(bar)


          >>> Foo.bar()
          I'm a static method.


          ---------------------------------------------------------------

          上面的代碼我們還可以寫的更簡(jiǎn)便些:

          >>> class Foo:
                  str = "I'm a static method."

                  @staticmethod
                  def bar():
                      print Foo.str


          >>> Foo.bar()
          I'm a static method.


          或者

          >>> class Foo:
                  str = "I'm a static method."

                  @classmethod
                  def bar(cls):
                      print cls.str


          >>> Foo.bar()
          I'm a static method.


          OK,差不多就是這個(gè)樣子了。
          posted on 2008-11-22 10:37 周銳 閱讀(411) 評(píng)論(0)  編輯  收藏 所屬分類: Python
          主站蜘蛛池模板: 志丹县| 拜泉县| 揭阳市| 永修县| 钟祥市| 如皋市| 江西省| 军事| 芒康县| 华容县| 九龙城区| 拉萨市| 南乐县| 石阡县| 青海省| 红安县| 平南县| 乐都县| 永登县| 濮阳市| 沾化县| 郯城县| 湘阴县| 淮滨县| 淄博市| 乌拉特前旗| 弥渡县| 华容县| 惠安县| 介休市| 平安县| 汉川市| 盘锦市| 渭源县| 龙南县| 宝鸡市| 大石桥市| 长宁县| 河曲县| 安吉县| 吴旗县|