In [48]: class TestStaticMethod():
....: @staticmethod
....: def foo():
....: print'static foo is called'
....: @classmethod
....: def foo2(cls):
....: print'class foo2 is called'
....: print'foo2 is part of calss',cls.__name__
....:
In [49]: Te
Templates/ TestStaticMethod
In [49]: TestStaticMethod.foo()
static foo is called
In [50]: TestStaticMethod.foo2()
class foo2 is called
foo2 is part of calss TestStaticMethod
In [51]: test = TestStaticMethod()
In [52]: test.foo()
static foo is called
In [53]: test.foo2()
class foo2 is called
foo2 is part of calss TestStaticMethod
總結:
1 三種方法,除了實例方法只能被實例調用外,其余對于類和實例均可以被調用
2 實例方法第一個參數self
靜態方法對參數無要求
類方法的第一個參數為類,一般用cls表示