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))
原文地址:http://blog.51cto.com/6000734/2313272
时间: 2024-10-22 09:00:15