class Test(object):
 
    __instance = None
 
    def __init__(self):
        print("----init方法----")

    def __new__(cls):
        print("----new方法----")
        if cls.__instance == None:
            cls.__instance = object.__new__(cls)
        return cls.__instance

test1 = Test()
print(id(test1))

test2 = Test()
print(id(test2))

#单例模式id值是一样的


 

 posted on 2022-02-21 17:20  boye169  阅读(11)  评论(0编辑  收藏  举报