Python_方法演示

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



posted @ 2017-05-31 14:07  JustLittle  阅读(410)  评论(0编辑  收藏  举报