from math import pi
class Color:
def __init__(self, red=0, green=0, blue=0):
self.red = red
self.green = green
self.blue = blue
# no abstract classes in Python!
class Shape:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def draw(self):
raise "abstract method"
def getArea(self):
raise "abstract method"
def move(self, x, y):
self.x += x
self.y += y
class Circle(Shape):
def __init__(self, x, y, radius, color):
Shape.__init__(self, x, y, color)
self.radius = radius
def draw(self):
pass # do something here
def getArea(self):
return int(pi * self.radius)
class Rectangle(Shape):
def __init__(self, x, y, width, height, color):
Shape.__init__(self, x, y, color)
self.width = width
self.height = height
def draw(self):
pass # do something here
def getArea(self):
return self.width * self.height
## Test the Classes ##
BLACK = Color()
WHITE = Color(255, 255, 255)
circle = Circle(100, 100, 50, BLACK)
rect = Rectangle(20, 20, 5, 5, WHITE)
circle.move(10, 10)
circle.x = 50
circle.radius = 5
print circle.getArea() # => 15
print rect.getArea() # => 25
print rect.width # => 5
rect.width = 10
print rect.width # => 10
circle.draw()
rect.draw()