我有几个相互延伸的课程。然后我有另一个类调用重写方法_draw(self.turtle)
。
班级结构:
class Canvas:
def __init__(self,w,h):
self.width = w
self.height = h
self.visibleObjects = []
self.turtle = turtle.Turtle()
self.screen = turtle.Screen()
self.screen.setup(width=self.width,height=self.height)
self.turtle.hideturtle()
def draw(self,gObject):
gObject.setCanvas(self)
gObject.setVisible(True)
self.turtle.up()
self.screen.tracer(0)
gObject._draw(self.turtle)
self.screen.tracer(1)
self.addShape(gObject)
class GeometricObject:
def __init__(self):
self.lineColor = 'black'
self.lineWidth = 1
self.visible = False
self.myCanvas = None
// setters and getters
class Shape(GeometricObject):
def __init__(self, fillColor = None):
super().__init__()
self.fillColor = fillColor
def setFill(self, aturtle):
aturtle.begin_fill()
aturtle.down()
aturtle.color(self.fillColor)
class Polygon(Shape):
def __init__(self, cornerPoints, color, lineColor, lineWidth):
super().__init__(color)
self.cornerPoints = cornerPoints
def _draw(self, aturtle):
// Start Drawing
class Triangle(Polygon):
def __init__(self, threePoints, fillColor = None, lineColor = None, lineWidth = None):
super().__init__(threePoints, fillColor, lineColor, lineWidth)
if (lineColor is not None):
self.lineColor = lineColor
if(lineWidth is not None):
self.lineWidth = lineWidth
def _draw(self, aturtle):
if (self.fillColor is not None):
self.setFill(aturtle)
aturtle.up()
Polygon._draw(self, aturtle)
aturtle.end_fill()
myCanvas = Canvas(800,600)
triangle = Triangle([Point(-50, -10), Point(150,25), Point(50,50)], "red", "yellow")
myCanvas.draw(triangle)
调用myCanvas.draw(triangle)
时,它会在Canvas类中执行draw方法。在第6行绘制方法中,我调用实际类的_draw方法gObject._draw(self.turtle)
。当我在此时调试时,gObject的类型为Triangle。因此,当执行这行代码时,我希望控件转到Triangle的_draw()。然而,控件移动到Polygon的_draw()并且控件永远不会出现在Triangle的_draw()中。
我不明白为什么要执行Polygon的_draw()?有人可以帮帮我吗?代码中是否缺少某些内容。
p.s:我有多个几何对象类,如Rectangle,Square,它们可以扩展Polygon等。
答案 0 :(得分:1)
如评论中所述,您的缩进是错误的。例如:
class Shape(GeometricObject):
def __init__(self, fillColor = None):
super().__init__()
self.fillColor = fillColor
def setFill(self, aturtle):
aturtle.begin_fill()
aturtle.down()
aturtle.color(self.fillColor)
不定义Shape.setFill
方法,而是仅存在于setFill
方法中的Shape.__init__
函数。因此,出于同样的原因,以下代码不会覆盖_draw
方法:
class Triangle(Polygon):
def __init__(self, threePoints, fillColor = None, lineColor = None, lineWidth = None):
super().__init__(threePoints, fillColor, lineColor, lineWidth)
if (lineColor is not None):
self.lineColor = lineColor
if(lineWidth is not None):
self.lineWidth = lineWidth
def _draw(self, aturtle):
if (self.fillColor is not None):
self.setFill(aturtle)
aturtle.up()
Polygon._draw(self, aturtle)
aturtle.end_fill()
对于“琐事”,请注意Python输入不能用作Java或其他类型。特别是,除非您明确定义,否则没有强制转换。
因此,如果您按如下方式定义Mother
和Daughter
:
class Mother:
def __init__(self):
pass
def printClass(self):
print(self.__class__.__name__)
class Daughter(Mother):
def __init__(self):
super().__init__()
在"Mother"
对象上调用printClass
时,没有直接的方法可以打印Daughter
。
在Java中,类似于:
(Mother)Daughter.printClass();
会打印"Mother"
,但你不能在Python中做这些事情。
换句话说,可以使用不同类型的对象重新分配变量,但是如果不重新分配变量,则无法更改变量的类型。
顺便说一句,解决方法是在castToMother
类中定义Daughter
方法,或类似的方法。
这个解释的目的是确保如果您知道triangle
对象的类型为Triangle
,则triangle._draw
无法调用Polygon._draw
。