|
发表于 2020-7-13 14:48:04
|
显示全部楼层
本帖最后由 qiuyouzhi 于 2020-7-13 14:50 编辑
你的clsmeth1并没有打印结果,当然不会输出objcnt1
代码改成这样:
- class ClsMethod():
- objcnt1 = 0
- @classmethod
- def clsmeth1(cls):
- cls.objcnt1 += 1
- cnt1 = cls.getobjcnt() #类方法中通过cls访问类方法
- cnt2 = ClsMethod.getobjcnt() # 类方法中通过列明访问类方法
- return cnt2 # return cnt1也行
- @classmethod
- def getobjcnt(cls):
- print('in getobjcnt(cls)')
- return ClsMethod.objcnt1
- def __init__(self):
- ClsMethod.clsmeth1() # 实例方法中通过类名.方法名访问类方法
- self.__class__.clsmeth1() # 实例方法中通过self.__class__.方法名访问类方法
复制代码 |
|