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