|
| 1 | +# from shape_class import * |
| 2 | + |
| 3 | +class Shape: |
| 4 | + def __init__(self, color=None): |
| 5 | + self.color = color |
| 6 | + |
| 7 | + def get_color(self): |
| 8 | + return self.color |
| 9 | + |
| 10 | + def __str__(self): |
| 11 | + return self.get_color() + ' Shape' |
| 12 | + |
| 13 | +class Rectangle(Shape): |
| 14 | + def __init__(self, color, length, width): |
| 15 | + super().__init__(color) |
| 16 | + self.length = length |
| 17 | + self.width = width |
| 18 | + |
| 19 | + def get_area(self): |
| 20 | + return self.length * self.width |
| 21 | + |
| 22 | + def get_perimeter(self): |
| 23 | + return 2 * (self.length + self.width) |
| 24 | + |
| 25 | + def __str__(self): |
| 26 | + return self.get_color() + ' ' + str(self.length) + 'x' + str(self.width) + ' ' + type(self).__name__ |
| 27 | + |
| 28 | +from math import pi |
| 29 | +class Circle(Shape): |
| 30 | + def __init__(self, color, radius): |
| 31 | + super().__init__(color) |
| 32 | + self.radius = radius |
| 33 | + |
| 34 | + def get_area(self): |
| 35 | + return pi * self.radius ** 2 |
| 36 | + |
| 37 | + def get_perimeter(self): |
| 38 | + return 2 * pi * self.radius |
| 39 | + |
| 40 | +def print_shape_data(self): |
| 41 | + print('Shape: ', type(self).__name__) |
| 42 | + print('Color: ', self.get_color()) |
| 43 | + print('Area: ', self.get_area()) |
| 44 | + print('Perimeter:', self.get_perimeter()) |
| 45 | + |
| 46 | +shape = Shape('red') |
| 47 | +print('shape is', shape.get_color()) |
| 48 | + |
| 49 | +rect = Rectangle('blue', 6, 4) |
| 50 | +print('rect is', rect.get_color(), ' with area:', rect.get_area(), ' and perimeter:', rect.get_perimeter()) |
| 51 | + |
| 52 | +circ = Circle('green', 5) |
| 53 | +print('circ is', circ.get_color(), ' with area:', circ.get_area(), ' and perimeter:', circ.get_perimeter()) |
| 54 | + |
| 55 | +print('rect is a', type(rect).__name__) |
| 56 | +print('circ is a', type(circ).__name__, '\n') |
| 57 | + |
| 58 | + |
| 59 | +my_new_shape = Rectangle('yellow', 17, 9) |
| 60 | +print_shape_data(my_new_shape) |
| 61 | + |
| 62 | +print(type(my_new_shape)) |
| 63 | +print(my_new_shape) |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
0 commit comments