【发布时间】:2020-10-18 19:25:23
【问题描述】:
我尝试编写一个使用 Clickable 和 Rectangle 类的 Mixins 示例,它们是 Button 类的超类。
目前我使用:
class Clickable:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.clicks = 0
def click(self):
self.clicks = self.clicks + 1
class Rectangle:
def __init__(self, x0, y0, x1, y1, *args, **kwargs):
super().__init__(*args, **kwargs)
self.upper_right = (x0, y0)
self.lower_down = (x1, y1)
class Button(Clickable, Rectangle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
但我考虑使用:
class Clickable:
def __init__(self):
self.clicks = 0
def click(self):
self.clicks = self.clicks + 1
class Rectangle:
def __init__(self, x0, y0, x1, y1):
self.upper_right = (x0, y0)
self.lower_down = (x1, y1)
class Button(Clickable, Rectangle):
def __init__(self, x0, y0, x1, y1):
Rectangle.__init__(self, x0, y0, x1, y1)
Clickable.__init__(self)
由于某种原因,其中一个更好吗?
【问题讨论】:
标签: python python-3.x inheritance multiple-inheritance mixins