class person:
def __init__(self,x):
self.__age = 10;
def age(self):
return self.__age;
t = person(22)
# t.__age = 100
print(t.age())
最好的方法
class MyCls(object):
__weight = 50
@property #以访问属性的方式来访问weight方法
def weight(self):
return self.__weight
if __name__ == '__main__':
obj = MyCls()
print(obj.weight)
obj.weight = 12
Traceback (most recent call last):
50
File "C:/PythonTest/test.py", line 11, in <module>
obj.weight = 12
AttributeError: can't set attribute