##############################
#类方法
##############################
class Foo:
str = "I'm a static method."
@classmethod #方法修饰符――指明该方法为类方法
def bar(cls): #cls为类对象,可以通过他引用静态数据成员
print cls.str
Foo.bar() #静态方法的调用方法一:类对象.方法名()
o1 = Foo()
o1.bar() #静态方法的调用方法二:类实例对象.方法名()
##############################
#静态方法
##############################
class Foo:
str = "I'm a static method."
@staticmethod
def bar(): #没有cls参数
print Foo.str
Foo.bar()
o1 = Foo()
o1.bar()
更多见
没有评论:
发表评论