码迷,mamicode.com
首页 > 编程语言 > 详细

Python高手之路【十二】面向对象设计模式

时间:2017-01-12 03:03:32      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:elf   对象   class   实例   print   self   log   设计模式   icm   

单例模式

单例,顾名思义单个实例。

class Person:
    __instance = None
    def __init__(self):
        pass
    @staticmethod
    def getInstance():
        if Person.__instance:
            return Person.__instance
        else:
            Person.__instance = Person()
            return Person.__instance
obj1 = Person.getInstance()
print(obj1)
obj2 = Person.getInstance()
print(obj2)
##################################################
<__main__.Person object at 0x0088A2D0>
<__main__.Person object at 0x0088A2D0>

 

Python高手之路【十二】面向对象设计模式

标签:elf   对象   class   实例   print   self   log   设计模式   icm   

原文地址:http://www.cnblogs.com/ginvip/p/6274211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!