|
发表于 2019-2-18 12:19:22
|
显示全部楼层
本帖最后由 darksolitary 于 2019-2-18 12:23 编辑
lass PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__[f.__name__] = f #将已存储完的函数名列表中的数据,通过循环添加到“owner”所拥有的__dict__(我理解是:用于存储自身方法的“字典类型:的属性),进而完成对象方法扩展
class AFeature(PlugIn):
def __init__(self):
super().__init__() #创建能存储自己所含的函数名列表
self._exported_methods.append(self.get_a_value) #存储
def get_a_value(self):
print ('a feature.')
class BFeature(PlugIn):
def __init__(self):
super().__init__()
self._exported_methods.append(self.get_b_value)
def get_b_value(self):
print ('b feature.')
class Combine:pass
c = Combine()
AFeature().plugin(c) #传递了两个对象
BFeature().plugin(c)
c.get_a_value()
c.get_b_value()
AFeature.__bases__ += (BFeature)
print(AFeature.__bases__) |
|