jacktangj 2020-01-01
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}
你是猪
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =