class A(object):
pass
class B(object):
b = 1
@classmethod
def test(cls):
print cls.b
def get_test(x):
try:
return x.test
except AttributeError:
return None
# 我这里只写了2个类,但是其实有很多类
for i in [A, B]:
test = get_test(i)
# 我要判断以下是否获得了这个类方法才能决定是否可以执行
if test:
test()
class Null(object):
def __init__(self, *args, **kwargs):
"忽略参数"
return None
def __call__(self, *args, **kwargs):
"忽略实例调用"
return self
def __getattr__(self, mname):
"忽略属性获得"
return self
def __setattr__(self, name, value):
"忽略设置属性操作"
return self
def __delattr__(self, name):
'''忽略删除属性操作'''
return self
def __repr__(self):
return "<Null>"
def __str__(self):
return "Null"
class Null(object):
def __init__(self, *args, **kwargs):
"忽略参数"
return None
def __call__(self, *args, **kwargs):
"忽略实例调用"
return self
def __getattr__(self, mname):
"忽略属性获得"
return self
def __setattr__(self, name, value):
"忽略设置属性操作"
return self
def __delattr__(self, name):
'''忽略删除属性操作'''
return self
def __repr__(self):
return "<Null>"
def __str__(self):
return "Null"
class Bridge(object):
def __init__(self):
self.__implementation = None
def someFunctionality(self):
raise NotImplemented()
class UseCase1(Bridge):
# 根据初始化参数传入实现的产品类
def __init__(self, implementation):
self.__implementation = implementation
# 根据传入的产品类的属性打印结果
def someFunctionality(self):
print "UseCase1: ",
self.__implementation.anotherFunctionality()
class UseCase2(Bridge):
def __init__(self, implementation):
self.__implementation = implementation
def someFunctionality(self):
print "UseCase2: ",
self.__implementation.anotherFunctionality()
class ImplementationInterface:
def anotherFunctionality(self):
raise NotImplemented
# 这里其实才是实现的产品类
class Linux(ImplementationInterface):
# 它定义了这个方法,回应操作系统的名字
def anotherFunctionality(self):
print "Linux!"
class Windows(ImplementationInterface):
def anotherFunctionality(self):
print "Windows."
def main():
linux = Linux()
windows = Windows()
useCase = UseCase1(linux)
useCase.someFunctionality()
useCase = UseCase1(windows)
useCase.someFunctionality()
useCase = UseCase2(linux)
useCase.someFunctionality()
useCase = UseCase2(windows)
useCase.someFunctionality()
if __name__ == "__main__":
main()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有