【发布时间】: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