python—单例模式
2018-08-19 本文已影响0人
Taoqi思
什么是单例模式:
确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,单例模式是一种对象创建型模式
class Dog(object)
__instance=None
def __new__(cls):
if cls.__instance==None:
cls.__instance=object.__new__(cls)
return cls.__instance
a= Dog()
print(a)
b = Dog()
print(b)
输出结果如下:
