【问题标题】:Initializing superclasses in Python在 Python 中初始化超类
【发布时间】:2013-03-22 22:30:04
【问题描述】:

我正在学习 Python。目前,我可以通过组合做我想做的事情,但是当我尝试使用继承做同样的事情时,我得到一个错误。这是我的代码。我基本上是在尝试为彩色方块开设课程。

from graphics import *

class Block(Rectangle):
    def __init__(self, corner, colour):
        self.corner = corner
        self.colour = colour
        self.x1 = self.corner.getX() * 30
        self.y1 = self.corner.getY() * 30
        self.x2 = self.x1 + 30
        self.y2 = self.y1 + 30
        self.point1 = Point(self.x1, self.y1)
        self.point2 = Point(self.x2, self. y2)
        Rectangle.__init__(self, self.point1, self.point2)


    def draw(self, window):
        self.window = window
        self.Rectangle.draw(self.window)


new_win = GraphWin("thingy", 700, 500)
corner = Point(1, 1)
square1 = Block(corner, 'red')
square1.draw(new_win)

new_win.mainloop()

我得到的错误是

File "F:\Python\4\4_3.py", line 24, in draw
self.draw(self.window)

错误无限重复。

当我使用组合时,这是我想做的代码:

from graphics import *

class Block():
    def __init__(self, corner, colour):
        self.corner = corner
        self.colour = colour
        self.x1 = self.corner.getX() * 30
        self.y1 = self.corner.getY() * 30
        self.x2 = self.x1 + 30
        self.y2 = self.y1 + 30
        self.point1 = Point(self.x1, self.y1)
        self.point2 = Point(self.x2, self. y2)
        self.Rectangle = Rectangle(self.point1, self.point2)

    def draw(self, window):
        self.window = window
        self.Rectangle.draw(self.window)
        self.Rectangle.setFill(self.colour)


new_win = GraphWin("thingy", 150, 150)
corner = Point(1, 1)
square1 = Block(corner, 'red')
square1.draw(new_win)

new_win.mainloop()

【问题讨论】:

  • 您实际上并没有显示导致错误的代码(对吗?)。或者错误。没有那个我们应该怎么回答? sscce.org
  • 你能把代码贴在你做继承的地方吗?
  • 我猜他希望它继承自 Rectangle...
  • 我很抱歉!我已经包含了错误,很快就会有有效的代码。

标签: python class inheritance initialization


【解决方案1】:
from graphics import *

class Block(Rectangle):
    def __init__(self, corner, colour):
        self.corner = corner
        self.colour = colour
        self.x1 = self.corner.getX() * 30
        self.y1 = self.corner.getY() * 30
        self.x2 = self.x1 + 30
        self.y2 = self.y1 + 30
        self.point1 = Point(self.x1, self.y1)
        self.point2 = Point(self.x2, self. y2)

        Rectangle.__init__(self, self.point1, self.point2)


    def draw(self, window):
        self.window = window
        Rectangle.draw(self, self.window)
        # instead of self.Rectangle.draw(self.window)

在继承的情况下,没有self.Rectangle

【讨论】:

  • 非常感谢!我可以发誓我试过了。如果 Block 已经初始化了 Rectangle,为什么还要在 Rectangle 上调用 draw 而不是 Block?程序不是将我的 Block 实例视为 Rectangle 的实例,并添加了 Rectangle.__init__ 上面的更改吗?
【解决方案2】:

python 2.7的简单代码是:

BaseClassName.__init__(self, args)

【讨论】:

  • 谢谢。据我所知,这正是我正在做的事情,但是当我调用 self.draw(self.window)、self.Rectangle.draw(self.window) 等时它会崩溃。因为我让它工作了与组合,我假设我误解了继承而不是绘制方法。
  • 如果你用block = Block(corner, colour)创建一个块,那么你必须做block.draw(window)来调用draw方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多