1 回答
TA贡献1804条经验 获得超2个赞
Square您可以通过将基类中使用的类作为SquareCollection集合类的属性来实现此目的,这样子类也可以显式覆盖它,而不是进行硬编码:
# Module_A
class Square:
pass # implement stuff here
class SquareCollection
BaseItem = Square # the "class that is collected here"
def __init__(self, dim_list):
# spawn BaseItem instances here, which are Square by default,
# but might be set to something else in a subclass
self.element = [self.BaseItem(val) for val in dim_list]
# Module_B1
import Module_A as ma
class Square(ma.Square):
pass
class SquareCollection(ma.SquareCollection):
# override the collection's BaseItem with this module's Square class,
# but keep the rest of the SquareCollection code the same
BaseItem = Square
添加回答
举报