__属性为私有属性
在对象内部可以访问,外部不可访问
同样加__方法,为私有方法
class Women:
def __init__(self,name):
self.name=name
self.__age=18
def __secret(self):
print("%s的年龄是%d"%(self.name,self.__age))
xiaofang=Women("小芳")
print(xiaofang.__age)
xiaofang.__secret()
----------------
D:\Anaconda\python.exe D:/Pycharm/PycharmProjects/python面向对象基础/k09_私有属性和私有方法.py
Traceback (most recent call last):
File "D:/Pycharm/PycharmProjects/python面向对象基础/k09_私有属性和私有方法.py", line 9, in <module>
print(xiaofang.__age)
AttributeError: 'Women' object has no attribute '__age'
Process finished with exit code 1