您的位置:首页 > 编程语言 > Python开发

0005python中的静态方法和类方法

2017-02-07 16:49 309 查看
#!/usr/bin/env python
# coding=utf-8

__metaclass__ = type

class StaticMethod:
@staticmethod
def foo():
print "This is static method foo()."

class ClassMethod:
@classmethod
def bar(cls):
print "This is class method bar()."
print "bar() is part of class:", cls.__name__

if __name__ == "__main__":
static_foo = StaticMethod() #实例化
static_foo.foo() #实例调用静态方法
StaticMethod.foo() #通过类来调用静态方法
print "********"
class_bar = ClassMethod()
class_bar.bar()
ClassMethod.bar()

C:\Users\Administrator>python d:\aa.py
This is static method foo().
This is static method foo().
********
This is class method bar().
bar() is part of class: ClassMethod
This is class method bar().
bar() is part of class: ClassMethod
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: