我只有几个互相延伸的课程。然后,我有另一个类,它调用覆盖的方法_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方法的第六行,我正在调用实际类的_draw方法gObject._draw(self.turtle)。当我进行调试时,gObject的类型为Triangle。因此,当执行此行代码时,我希望控件进入Triangle的_draw()。但是,该控件移至Polygon的_draw(),而该控件从未移至Triangle的_draw()。

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

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

最佳答案

如评论中所述,您的缩进是错误的。例如:

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或其他类型一样工作。特别是,除非您明确定义它,否则就没有类型转换。
因此,如果您按以下方式定义MotherDaughter

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

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

10-11 22:03
查看更多