转自http://stonelee.javaeye.com/blog/726449
使用方法:
-
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
调用方法:
-
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)
区别:
-
类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
-
类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数
阅读(1415) | 评论(0) | 转发(0) |