首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

静态方法跟类成员方法

2012-12-20 
静态方法和类成员方法静态方法和类成员方法分别在创建时分别被装入Staticmethod类型和Classmethod类型的对

静态方法和类成员方法
静态方法和类成员方法分别在创建时分别被装入Staticmethod类型和Classmethod类型
的对象中。
静态方法的定义没有self参数,且能够被类本身直接调用。
类方法的定义时需要名为cls的类似于self的参数,类成员方法可以直接用类的具体对象
调用。但cls参数是自动被绑定到类的。

__metaclass__ = typeclass MyClass:    def smeth():        print 'This is a static method'    smeth = staticmethod(smeth)    def cmeth(cls):        print 'This is a class method of', cls    cmeth = classmethod(cmeth)

装饰器:
__metaclass__ = typeclass MyClass:    @staticmethod    def smeth():        print 'This is a static method'    @classmethod    def cmeth(cls):        print 'This is a class method of', cls

热点排行