我正在學習Python。目前,我可以通過構圖來做我想做的事情,但是當我嘗試使用繼承來做同樣的事情時,我得到一個錯誤。這是我的代碼。我基本上是想爲彩色方塊做一個班。在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()
你還沒有實際顯示代碼導致錯誤(對嗎?)。或者錯誤。我們應該如何回答沒有? http://sscce.org/ – delnan 2013-03-22 22:31:33
你可以將代碼發佈到繼承的位置嗎? – israelord 2013-03-22 22:33:36
我想他希望它從矩形繼承... – Korem 2013-03-22 22:46:48