class Person(object):
def run(self):
print(self.__money)
print("run")
def eat(self,food):
print("eat"+food)
def __init__(self,name,age,height,weight,money):
self.name = name
self._height = height
self.age =age
self.weight = weight
self.__money = money
def setMoney (self,money):
#数据的过滤
#通过自定义的方法实现对私有属性的赋值与取值
if money< 0:
money = 0
self.__money += money
def getMoney(self, money):
return self.__money
per = Person("hanmeimie ",20,180,80,10000)
# per.age = 10
# print(per.age)
#如果要让内部的属性不被外部直接访问,在属性前加两个下划线 __
#在python中,如果在属性前加__两个下划线,那么这个属性就变成了私有属性
# per.__money = 0
# per.run()
#不能直接访问per.__money是因为Python解释器把__money变成了__Person__money
#仍然可以用__Person__money去访问,但是强烈建议不要这么干
#不同版本解释器可能存在解释的变量名不一致
per._Person__money=1
# print(per.getMoney())
#在python中 __xxx__变量,这样的实例变量外部是可以访问的,
# 但是按照约定的规则,放我们看到这样的变量是,
# 意思是我可以被访问,但是把我视为私有变量
print(per._height)
访问限制【学习笔记】
猜你喜欢
转载自blog.csdn.net/qq_41856814/article/details/89367234
今日推荐
周排行