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

来源:互联网 发布:苹果的录音软件 编辑:程序博客网 时间:2024/06/15 20:37

两种声明方式:

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()