|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class PlugIn(object):
- def __init__(self):
- self._exported_methods = []
-
- def plugin(self, owner):
- for f in self._exported_methods:
- owner.__dict__[f.__name__] = f
- def plugout(self, owner):
- for f in self._exported_methods:
- del owner.__dict__[f.__name__]
- class AFeature(PlugIn):
- def __init__(self):
- super(AFeature, self).__init__()
- self._exported_methods.append(self.get_a_value)
- def get_a_value(self):
- print('a feature.')
- class BFeature(PlugIn):
- def __init__(self):
- super(BFeature, self).__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()
复制代码
没有理解具体怎么运作,求大佬尽量详细解析。。。
- c = Combine()
- AFeature().plugin(c)
- class AFeature(PlugIn):
- def __init__(self):
- super(AFeature, self).__init__()
- class PlugIn(object):
- def __init__(self):
- self._exported_methods = []
- self._exported_methods.append(self.get_a_value)
- def plugin(self, owner): # 这里的owner,就是Combine的实例化对象
- for f in self._exported_methods:
- owner.__dict__[f.__name__] = f
复制代码
|
|