import math
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def move(self,x,y):
self.x += x
self.y += y
def __str__(self):
return "<"+ str(self.x) + "," + str(self.y) + ">"
class Shape:
def __init__(self, centrePoint, colour, width, height):
self.centrePoint = centrePoint
self.colour = colour
self.width = width
self.height = height
self.type = "Square"
def __init__(self, centrePoint, radius, colour):
self.type = "Circle"
self.radius = radius
self.colour = colour
self.centrePoint = centrePoint
def move(self,x,y):
self.centrePoint.move(x,y)
def getArea(self):
if (self.type == "Square"):
return self.width * self.height
elif (self.type == "Circle"):
return math.pi*(self.radius**2)
def __str__(self):
return "Center Point: " + str(self.centrePoint) + "\nColour: "+ self.Colour + "\nType: " + self.type + "\nArea: " + self.getArea()
class Rectangle (Shape):
def scale(self, factor):
self.scaleVertically(factor)
self.scaleHorizontally(factor)
def scaleVertically(self, factor):
self.height *= factor
def scaleHorizontally(self, factor):
self.width *= factor
class Circle (Shape):
def scale(self, factor):
self.radius * factor
這是我到目前爲止,形狀應該代表一個抽象類,其他兩個類都應該從它繼承的代碼,對我來說它仍然看起來太硬編碼爲一個抽象的解決方案,我怎麼能改進?Python的抽象編程
這是http://codereview.stackexchange.com/的近乎完美的問題。 (ETA:請參閱[代碼評論常見問題](http://codereview.stackexchange.com/faq)瞭解它與StackOverflow的不同之處 - 通常詢問如何改進您的* working *代碼屬於前者) – 2012-04-22 16:42:05
可以嗎暫時留在這裏?我可能需要問一些功能性問題。 – JamieB 2012-04-22 16:45:10
有關此特定代碼的功能性問題會更好地問*和*更有可能在代碼評審中回答。如果除了「我怎樣才能讓這段代碼變得更好」之外,還有*新的*問題,那麼他們應該在StackOverflow上單獨提出問題。(同樣,你的問題沒有錯,我只是建議一個網站,你可以得到更有幫助的解決方案) – 2012-04-22 16:46:58