class Root:
__total=0
def __init__(self,v): #构造函数
self.__value=v
Root.__total+=1
def show(self): #普通实例方法
print('self.__value:',self.__value)
print('Root.__total:',Root.__total)

@classmethod #修饰器,声明类方法
def classShowTotal(cls):
print(cls.__total)

@staticmethod
def staticsShowTotal():
print(Root.__total)
r=Root(3)
r.classShowTotal()
r.staticsShowTotal()
r.show()
rr=Root(5)
Root.classShowTotal()
Root.staticsShowTotal()
Root.show(r)
r.show()
Root.show(rr)
rr.show()


###########################输出###########################

1
1
self.__value: 3
Root.__total: 1
2
2
self.__value: 3
Root.__total: 2
self.__value: 3
Root.__total: 2
self.__value: 5
Root.__total: 2
self.__value: 5
Root.__total: 2



相关文章:

  • 2021-12-06
  • 2022-12-23
  • 2021-10-22
  • 2021-11-11
  • 2021-10-17
  • 2021-09-06
  • 2021-04-01
  • 2022-12-23
猜你喜欢
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2021-09-14
  • 2021-12-28
  • 2021-07-06
相关资源
相似解决方案