比如说,我有一些MainClass
,在它的方法中的某个点上,OtherClass
的对象的实例化作为MainClass
的属性。你知道吗
mainobj = MainClass()
isinstance(mainobj.otherclassobj, OtherClass) == True
现在,我想扩展OtherClass
,然后将MainClass
与新的扩展类一起使用。你知道吗
除了扩展MainClass
和重新定义所有实例化self.otherclassobj
的方法以从ExtendedOtherClass
开始之外,我还有更方便的选择吗?你知道吗
如果可以稍微更改
MainClass
的实现,则不需要从中继承。就吃点像默认情况下,
MainClass
将使用OtherClass
,但您可以将ExtendedOtherClass
作为关键字参数传入。你知道吗如果您已经不需要
OtherClass
,那么可以执行以下操作如果两个类都需要,或者逻辑/方法都需要更改,则需要重新实现/扩展
MainClass
。你知道吗相关问题 更多 >
编程相关推荐