1 class Demo(): 2 __num = 0 # 私有属性 3 result = 0 # 公开属性 4 5 def count(self): 6 self.__num += 1 7 self.result += 1 8 print(self.__num) 9 10 def getnum(self): 11 return self.__num 12 13 run = Demo() 14 run.count() 15 print(run.result) 16 # print(run.__num) # 报错,实例不能访问私有变量 17 __num = run.getnum() 18 print(__num)
标签:__,run,私有,self,num,print,属性 From: https://www.cnblogs.com/frazer-cxy-jsfx/p/17261028.html