classmethod类方法 分类: python 小练习 python基础学习 2014-02-07 10:36 214人阅读 评论(0) 收藏

类方法可以直接使用类名调用;否则只能使用实例进行调用方法

#coding:utf-8
'''
类方法可以直接使用类名调用;否则只能使用实例进行调用方法

'''
class c:
    def __init__(self):
        self._a=1
    @classmethod
    def t(self):
        print self
    def t2(self):
        print self

cc=c()
c.t() # __main__.c
c.t2() #报错
cc.t2() # <__main__.c instance at 0x04C7D828>
cc.t() # __main__.c
cc.t2() #<__main__.c instance at 0x04C7D918>


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/think1988/p/4627948.html