Created
August 27, 2011 03:38
-
-
Save ChristopherMacGown/1174934 to your computer and use it in GitHub Desktop.
bj's homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import math | |
>>> | |
>>> | |
>>> class Shape(object): | |
... def area(self): | |
... raise NotImplementedError("An arbitrary shape doesn't have a defined " | |
... "area. Please use a descendent class.") | |
... | |
>>> | |
>>> class Circle(Shape): | |
... def __init__(self, radius): | |
... self.radius = radius | |
... | |
... def area(self): | |
... return 2 * math.pi * self.radius | |
... | |
>>> | |
>>> class Rectangle(Shape): | |
... def __init__(self, width, height): | |
... self.width = width | |
... self.height = height | |
... | |
... def area(self): | |
... return self.width * self.height | |
... | |
>>> | |
>>> class Square(Rectangle): | |
... def __init__(self, width): | |
... super(Square, self).__init__(width, width) | |
... | |
>>> | |
>>> Circle(10).area() | |
62.83185307179586 | |
>>> Rectangle(10,20).area() | |
200 | |
>>> Square(10).area() | |
100 | |
>>> Rectangle(10,10).area() | |
100 | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment