Python: 如何调用子 class 的重写方法
Python: How to call a child class's overridden method
我有几个 class 可以相互扩展。然后我有另一个 class 调用重写方法 _draw(self.turtle)
.
Class结构:
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)
时,会执行Canvasclass中的draw方法。在 draw 方法的第 6 行,我调用了实际的 class 的 _draw 方法 gObject._draw(self.turtle)
。当我此时调试时,gObject 是 Triangle 类型。因此,当执行这行代码时,我希望控件转到 Triangle 的 _draw()。然而,控件移动到多边形的_draw() 并且控件永远不会到达三角形的_draw()。
我不明白为什么要执行Polygon的_draw()?有人可以帮我吗?代码中是否缺少某些内容。
p.s:我有多个几何对象 class,例如矩形、扩展多边形的正方形等
如评论中所述,您的缩进是错误的。例如:
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()
对于 "trivia",请注意 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__()
在 Daughter
对象上调用 printClass
时,没有直接的方法来打印 "Mother"
。
在 Java 中,类似于:
(Mother)Daughter.printClass();
会打印 "Mother"
,但你不能在 Python 中做这样的事情。
换句话说,一个变量可以被重新赋值给一个不同类型的对象,但是如果不重新赋值就无法改变变量的类型。
顺便说一下,解决方法是在 Daughter
class 中定义一个 castToMother
方法,或者类似的方法。
这个解释的重点是让你确定,如果你知道你的 triangle
对象是 Triangle
类型,那么 triangle._draw
就不可能调用 Polygon._draw
。
我有几个 class 可以相互扩展。然后我有另一个 class 调用重写方法 _draw(self.turtle)
.
Class结构:
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)
时,会执行Canvasclass中的draw方法。在 draw 方法的第 6 行,我调用了实际的 class 的 _draw 方法 gObject._draw(self.turtle)
。当我此时调试时,gObject 是 Triangle 类型。因此,当执行这行代码时,我希望控件转到 Triangle 的 _draw()。然而,控件移动到多边形的_draw() 并且控件永远不会到达三角形的_draw()。
我不明白为什么要执行Polygon的_draw()?有人可以帮我吗?代码中是否缺少某些内容。
p.s:我有多个几何对象 class,例如矩形、扩展多边形的正方形等
如评论中所述,您的缩进是错误的。例如:
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()
对于 "trivia",请注意 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__()
在 Daughter
对象上调用 printClass
时,没有直接的方法来打印 "Mother"
。
在 Java 中,类似于:
(Mother)Daughter.printClass();
会打印 "Mother"
,但你不能在 Python 中做这样的事情。
换句话说,一个变量可以被重新赋值给一个不同类型的对象,但是如果不重新赋值就无法改变变量的类型。
顺便说一下,解决方法是在 Daughter
class 中定义一个 castToMother
方法,或者类似的方法。
这个解释的重点是让你确定,如果你知道你的 triangle
对象是 Triangle
类型,那么 triangle._draw
就不可能调用 Polygon._draw
。