class Name: __a=‘你是猪‘ #封装变量a def __init__(self,name): self.name=name def get_name(self): print(‘我叫%s‘ %self.name) n1=Name(‘陈宇霞‘)print(Name.__dict__) #查看类属性字典print(n1._Name__a) #可以通过此种方式调用__a ,没有真正的封装 #执行结果:
{‘__module__‘: ‘__main__‘, ‘_Name__a‘: ‘你是猪‘, ‘__init__‘: <function Name.__init__ at 0x00000242BD968EA0>, ‘get_name‘: <function Name.get_name at 0x00000242BD968E18>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Name‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Name‘ objects>, ‘__doc__‘: None}
你是猪
原文地址:https://www.cnblogs.com/chenyuxia/p/12130446.html
时间: 2024-10-08 19:43:27