class Woman:
def __init__(self, name):
self.name = name
self.__age = 24 # 私有熟悉
def __secret(self):
'''
私有方法
:return:
'''
print("%s is %d years old." % (self.name, self.__age))
if __name__ == '__main__':
Lili = Woman("Lili")
# 外部不能直接访问私有属性
#print(Lili.age)
# 但能通过类进行访问,但工作中不建议使用
print(Lili._Woman__age)
# 外部不能直接访问私有方法
#Lili.secret()
# 但能通过类进行访问,但工作中不建议使用
Lili._Woman__secret()
python 面向对象 私有熟悉和私有方法
最新推荐文章于 2024-07-28 15:46:11 发布