gpt4 book ai didi

Python:如何调用子类的重写方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:55 25 4
gpt4 key购买 nike

我有一些相互扩展的类(class)。然后我有另一个类调用重写方法 _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方法。在draw方法的第6行,我调用实际类的_draw方法gObject._draw(self.turtle)。当我此时调试时,gObject 是 Triangle 类型。因此,当执行这行代码时,我希望控件转到 Triangle 的 _draw()。然而,控件移动到 Polygon 的 _draw(),而控件永远不会到达 Triangle 的 _draw()。

我不明白为什么它要执行 Polygon 的 _draw() ?有人可以帮我吗?代码中是否缺少某些内容。

p.s:我有多个几何对象类,如矩形、正方形,它们扩展了多边形等。

最佳答案

正如评论中提到的,您的缩进是错误的。例如:

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 方法,而是定义仅存在于 Shape.__init__ 方法中的 setFill 函数。因此,出于同样的原因,以下代码不会重写 _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 或其他类型那样工作。特别是,除非您明确定义它,否则不存在强制转换。因此,如果您按如下方式定义 MotherDaughter:

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 类内部定义一个 castToMother 方法,或者类似的方法。

这个解释的目的是让您确定,如果您知道您的 triangle 对象是 Triangle 类型,则 triangle._draw 将调用 Polygon._draw

关于Python:如何调用子类的重写方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40518539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com