现在的位置: 首页 > 综合 > 正文

py 类方法和静态方法的声明

2013年09月06日 ⁄ 综合 ⁄ 共 381字 ⁄ 字号 评论关闭

两种声明方式:

1.在类中声明一个函数 然后 foo = staticmethod(foo)
2.在函数声明定义的前一行加  @staticmethod

class TestStaticMethon:
 @staticmethod
     def foo():
            print 'static methond'
   #foo = staticmethod(foo)
 
class TestClassMethon:
 @classmethod
   def foo(cls):
       print 'foo() is the class method'

  #foo = classmethod(foo)

tsm = TestStaticMethon()
TestStaticMethon.foo()
tsm.foo()

 

 

tc = TestClassMethon()
TestClassMethon.foo()
tc.foo()

抱歉!评论已关闭.