鱼C论坛

 找回密码
 立即注册
查看: 1611|回复: 1

关于小甲鱼类和对象拾遗那一课视频中提到的扩展阅读Mixin

[复制链接]
发表于 2020-1-19 17:00:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()

'''owner.__dict__[f.__name__] = f
其中这一句的用法在以前课程中没有提及过,无法理解,导致整个代码读不太懂,有大神能帮忙解释下吗'''
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-1-19 21:32:41 | 显示全部楼层
设置 owner 中名字为 f.__name__(即 f 的类型名,例如 str.__name__ 是 'str') 的属性的值为 f。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-21 21:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表