应用场景python
定义方式spa
code
class Women: def __init__(self, name): self.name = name # 不要问女生的年龄 self.__age = 18 def __secret(self): print("个人年龄是 %d" % self.__age) xiaofang = Women("小芳") # 私有属性,外部不能直接访问 # print(xiaofang.__age) # 私有方法,外部不能直接调用 # xiaofang.__secret()
提示:在平常开发中,不要使用这种方式,访问对象的 私有属性 或 私有方法对象
Python
中,并无 真正意义 的 私有blog
_类名
=> _类名__名称
# 私有属性,外部不能直接访问到 print(xiaofang._Women__age) # 私有方法,外部不能直接调用 xiaofang._Women__secret()