timmy ревизий этого фрагмента 10 months ago. К ревизии
1 file changed, 17 insertions
colored_graphic_mixin.py(файл создан)
| @@ -0,0 +1,17 @@ | |||
| 1 | + | class GraphicMixin: | |
| 2 | + | def draw(self): | |
| 3 | + | print("Drawing the graphic") | |
| 4 | + | ||
| 5 | + | class ColorMixin: | |
| 6 | + | def set_color(self, color): | |
| 7 | + | self.color = color | |
| 8 | + | print(f"Setting color to {color}") | |
| 9 | + | ||
| 10 | + | class ColoredGraphic(ColorMixin, GraphicMixin): | |
| 11 | + | pass | |
| 12 | + | ||
| 13 | + | # 使用 ColoredGraphic 類 | |
| 14 | + | colored_graphic = ColoredGraphic() | |
| 15 | + | colored_graphic.draw() # 呼叫 GraphicMixin 的方法 | |
| 16 | + | colored_graphic.set_color("red") # 呼叫 ColorMixin 的方法 | |
| 17 | + | ||
Новее
Позже