设计模式

单例模式
class
MySingleton: __obj = None __init_flag = True def __new__(cls, *args, **kwargs): if not cls.__obj: cls.__obj = super().__new__(cls) return cls.__obj def __init__(self, name): if MySingleton.__init_flag: print("Init ......") MySingleton.__init_flag = False self.name = name s = MySingleton("nxr") print(s.name) s1 = MySingleton("jsm") # print(s.name) print(s1.name)

 

posted @ 2019-06-24 11:40  飞蝎儿  阅读(99)  评论(0编辑  收藏  举报