Last active
August 29, 2015 14:23
-
-
Save wagamama/3ec31216f2ba1e4dcdfe to your computer and use it in GitHub Desktop.
Clean Code - Ch. 6 Object Shape
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
| class Shape: | |
| def area(self): | |
| raise Exception | |
| class Square(Shape): | |
| def __init__(self, side): | |
| self._side = side | |
| def area(self): | |
| return self._side ** 2 | |
| class Rectangle(Shape): | |
| def __init__(self, width, height): | |
| self._width = width | |
| self._height = height | |
| def area(self): | |
| return self._width * self._height | |
| class Circle(Shape): | |
| def __init__(self, radius): | |
| self.PI = 3.14159 | |
| self._radius = radius | |
| def area(self): | |
| return self.PI * (self._radius ** 2) | |
| def main(): | |
| s = Square(5) | |
| print s.area() | |
| r = Rectangle(3, 8) | |
| print r.area() | |
| c = Circle(4) | |
| print c.area() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment