使用方法:
- class A(object):
- def foo(self,x):
- #类实例方法
- print "executing foo(%s,%s)"%(self,x)
- @classmethod
- def class_foo(cls,x):
- #类方法
- print "executing class_foo(%s,%s)"%(cls,x)
- @staticmethod
- def static_foo(x):
- #静态方法
- print "executing static_foo(%s)"%x
http://blog.163.com/yang_jianli/blog/static/161990006201122411586729/
调用方法:
Python代码
- a = A()
- a.foo(1) //print : executing foo(<__main__.A object at 0xb77d67ec>,1)
- a.class_foo(1) //executing class_foo(<class '__main__.A'>,1)
- A.class_foo(1) //executing class_foo(<class '__main__.A'>,1)
- a.static_foo(1) //executing static_foo(1)
- A.static_foo(1) //executing static_foo(1)
区别:
- 类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
- 类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数