class money:
def __init__(self):
self.__money=100 #__表示是私有变量
def __getmoney(self): #私有方法
print("getmoney")
return self.__money
def show(self):
print(self.__money)
aa = money()
print(dir(aa)) #查看aa对象的变量和方法
aa._money__money=100000 #访问私有变量
aa._money__getmoney() #访问私有方法,python私有变量的本质
aa.show()
Python 访问私有变量和私有方法
最新推荐文章于 2024-05-08 05:42:43 发布