Python案例-开发之路-设计模式-单例模式

来源:互联网 发布:安卓计步器软件哪个好 编辑:程序博客网 时间:2024/06/06 11:40
#!/usr/bin/env python# -- coding = 'utf-8' --# Author Allen Lee# Python Version 3.5.1# OS Windows 7#设计模式,单例模式,单实例class Foo:    instance = None    def __init__(self,name):        self.name = name    @classmethod    def get_instance(cls):        if cls.instance:            return cls.instance        else:            obj = cls('alex')            cls.instance = obj            return objobj1 = Foo.get_instance()print(obj1)obj2 = Foo.get_instance()print(obj2)
0 0
原创粉丝点击