关键词搜索

源码搜索 ×
×

Python笔记-类的单例模式及重载打印

发布2021-09-28浏览402次

详情内容

重载如下类:

  1. class Position(object):
  2. def __init__(self, x = None, y = None):
  3. self.x = x
  4. self.y = y
  5. pass
  6. def getX(self):
  7. return self.x
  8. def getY(self):
  9. return self.y
  10. def setX(self, x):
  11. self.x = x
  12. pass
  13. def setY(self, y):
  14. self.y = y
  15. pass
  16. def __repr__(self):
  17. return '(%d,%d)'%(self.x if self.x else -1, self.y if self.y else -1)
  18. pass
  19. pass

Python中的单例:

  1. class CvGraph(object):
  2. instanceLock = threading.Lock()
  3. def __init__(self):
  4. pass
  5. def __new__(cls, *args, **kwargs):
  6. if not hasattr(CvGraph, "instance"):
  7. with CvGraph.instanceLock:
  8. if not hasattr(CvGraph, "instance"):
  9. CvGraph.instance = object.__new__(cls)
  10. pass
  11. pass
  12. pass
  13. return CvGraph.instance
  14. pass
  15. pass

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载