-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_draw.py
More file actions
57 lines (45 loc) · 1.19 KB
/
Copy pathshape_draw.py
File metadata and controls
57 lines (45 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from abc import ABC, abstractmethod
import math
# Abstract Class
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def draw(self):
pass
# Subclass - Circle
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def draw(self):
print("Drawing a Circle")
# Subclass - Rectangle
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def draw(self):
print("Drawing a Rectangle")
# Subclass - Triangle
class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height
def area(self):
return 0.5 * self.base * self.height
def draw(self):
print("Drawing a Triangle")
# Creating Objects
circle = Circle(5)
rectangle = Rectangle(4, 6)
triangle = Triangle(3, 8)
# Display Results
shapes = [circle, rectangle, triangle]
for shape in shapes:
shape.draw()
print("Area:", shape.area())