Python单利模式,简单工厂实现,import自定义模块
admin
2023-07-17 09:43:03
0

import mypackage.ZString as zs
'''
单利模式
'''

class User(object):
__instance = None

def __init__(self, name, age):
    self.name = name
    self.age = age
    print('name=%s,age=%s' % (self.name, self.age))

def __new__(cls, name, age):
    if not cls.__instance:
        cls.__instance = object.__new__(cls)
    return cls.__instance

u1 = User('tom', 18)
u2 = User('jack', 28)

print(u1 == u2)
print(id(u1))
print(id(u2))

'''
工厂模式
'''

class Person(object):
def init(self, name):
self.name = name

def work(self, axe_type):
    print('%s开始工作了' % self.name)
    # axe=StoneAxe('花岗岩')
    # axe=SteelAxe('加爵')
    axe = Factory.get_axe(axe_type)
    if not axe == None:
        axe.cut_tree()

class Axe(object):
def init(self, name):
self.name = name

def cut_tree(self):
    print('%s进行砍树了' % self.name)

class StoneAxe(Axe):
def cut_tree(self):
super().cut_tree() # 子类调用父类方法
print('使用石头做的斧头砍树')

class SteelAxe(Axe):

def cut_tree(self):
    super().cut_tree()  # 子类调用父类方法
    print('使用钢铁做的斧头砍树')

class Factory(object):

静态简单工厂方法

@staticmethod
def get_axe(axe_type):
    if axe_type == "stone":
        return StoneAxe('花岗岩')
    elif axe_type == "steel":
        return SteelAxe('加爵')
    else:
        print('你输入错误吧')

p = Person('张三')
p.work('steel')

def get_str():
s=None
print(zs.isnull(s))

get_str()

字符串为None时为True,为空时也True,否则为False

def isnull(str):
if not str:
return True
elif str.strip() == '':
return True
else:
return False

if name == 'main':
t=None
print(isnull(t))

相关内容

热门资讯

普京回应泽连斯基公开信 6月5日,俄罗斯总统普京就乌克兰总统泽连斯基日前发布的公开信作出点评,称暂时看不到与基辅政权领导人会...
向新提质 青岛加快建设新型电力... 能源互联网是支撑新型能源体系的重要基础设施与关键枢纽,对于保障能源安全、推动绿色转型以及培育新质生产...
Phison展示AI存储新布局... 【CNMO科技消息】在2026年中国台北国际电脑展Computex期间,存储解决方案厂商Phison...
2026苹果手机播放器推荐 2026年适合苹果手机的主流播放器网易爆米花https://bmh.163.com/windows/...
“艺考名师”涉嫌性侵猥亵至少2... 今年4月,影视艺考培训机构影路站台创始人杜某哲涉嫌强奸和强制猥亵一案开庭。庭审持续了整整两天。据悉,...
又一项目签约东莞,重点打造高端... 6月5日,东莞水乡经济区管委会 麻涌镇人民政府 与广合科技股份有限公司 举行项目投资签约仪式 广合科...
技术扎根,全栈破局!科陆电子S... 6月3日-5日,SNEC2026在上海国家会展中心举行。本次展会是美的集团新能源事业部成立后的首次亮...
合肥市长江路第二小学橡树湾学子... 以科技筑梦,以创新成长!近日,2026年合肥市机器人竞赛圆满落幕。全市各校科创能手同台竞技、巅峰对决...
增至200颗!我国成功发射千帆... 2026年6月5日14时34分,在海南商业航天发射场,长征八号运载火箭将千帆极轨12组卫星准确送入预...
苏州医疗机构AI智能体应用实践... 人工智能正深刻重塑卫生健康服务模式,AI智能体已成为医院数字化转型、智慧医疗建设的重要引擎。为了让A...