|
发表于 2014-9-2 11:37:11
|
显示全部楼层
- class PlugIn(object): #定义一个PiugIn的类
- def __init__(self): #定义__init__方法
- self._exported_methods = [] #设置局部变量 _exported_methods
-
- def plugin(self, owner): #定义plugin的方法
- for f in self._exported_methods: #把列表都列出来。
- owner.__dict__[f.__name__] = f #__dict__用于扩展对象方法。
- def plugout(self, owner): #这个不知道什么用。。删掉完全可以运行。
- for f in self._exported_methods:
- del owner.__dict__[f.__name__]
-
- class AFeature(PlugIn): #定义AFeature的类是PlugIn的子类
- def __init__(self): #定义__init__方法
- super(AFeature, self).__init__() #继承父类的方法
- self._exported_methods.append(self.get_a_value) #_exported_methods列表里添加get_a_value
- def get_a_value(self): #定义get_a_value方法
- print ('a feature.') #方法是打印打印a feature.
- class BFeature(PlugIn): #定义BFeature类是PlugIn的子类
- def __init__(self): #定义__init__方法
- super(BFeature, self).__init__() #继承父类的方法
- self._exported_methods.append(self.get_b_value) #_exported_methods列表里添加get_b_value
-
- def get_b_value(self): #定义get_b_value方法
- print ('b feature.') #方法是打印打印b feature.
- class Combine:pass #定义Combine类,里面什么都木有。。
- c = Combine() #实例对象c
- AFeature().plugin(c) #使c扩展对象方法。
- BFeature().plugin(c) #使c扩展对象方法。
- c.get_a_value() #调用方法。
- c.get_b_value()
复制代码
刚看完39讲。。有啥不对的还请指正。
|
|