在实例化对象时,是否可以传入一个类作为对象的基类?
例如:
class Red(object):
def x(self):
print '#F00'
class Blue(object):
def x(self):
print '#00F'
class Circle(object):
def __init__(self, parent):
# here, we set Bar's parent to `parent`
self.x()
class Square(object):
def __init__(self, parent):
# here, we set Bar's parent to `parent`
self.x()
self.sides = 4
red_circle = Circle(parent=Red)
blue_circle = Circle(parent=Blue)
blue_square = Square(parent=Blue)
这将产生类似于以下效果:
class Circle(Red):
def __init__(self):
self.x()
但又不影响其他Circle
实例。