这是Python多层继承的一个特例,祖父,父亲,儿子都有 draw 方法,那么经过多次继承后,
- 如何对于不同层次的方法传递参数呢,可以看这篇文章python super 理解(四)
- 如何对于不同层次的方法传递参数呢,那么这个例子展现了一种解法,但是这种做法不够通用,在下一篇文章我给出更加通用的玩法
def myFun(ff,**kwargs):
# print(kwargs)
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
print('#'*12)
# Driver code
myFun(ff ='a',first ='Geeks', mid ='for', last='Geeks')
# input_dict={'first' :'Geeks', 'mid' :'for', 'last':'Geeks'}
# myFun(**input_dict)
first == Geeks
mid == for
last == Geeks
############
class Root:
def draw(self):
# the delegation chain stops here
print('Root Drawing draw ')
assert not hasattr(super(), 'draw')
class Shape(Root):
def __init__(self, shapename, **kwds):
self.shapename = shapename
# print('Shape class',kwds)
super().__init__(**kwds)
def draw(self):
print('Shape Drawing. Setting shape to:', self.shapename)
super().draw()
class ColoredShape(Shape):
def __init__(self, color, **kwds):
self.color = color
# print('ColoredShape class',kwds)
super().__init__(**kwds)
def draw(self):
print('ColoredShape Drawing. Setting color to:', self.color)
super().draw()
cs = ColoredShape(color='blue', shapename='square')
cs.draw()
ColoredShape Drawing. Setting color to: blue
Shape Drawing. Setting shape to: square
Root Drawing draw