-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBridge.py
More file actions
63 lines (51 loc) · 1.74 KB
/
Copy pathBridge.py
File metadata and controls
63 lines (51 loc) · 1.74 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
58
59
60
61
62
63
#!/usr/bin/env python
# Written by: DGC
import abc
#==============================================================================
class Shape(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def __init__(self):
pass
def area(self):
"""
Returns the area of the shape calculated using the shape specific
implementation.
"""
assert self.calculator != None, "self.calculator not defined."
return self.calculator(self)
#==============================================================================
class Rectangle(Shape):
def __init__(self, x, y):
self.calculator = rectangular_area_calculator
self.x = x
self.y = y
#==============================================================================
def rectangular_area_calculator(rectangle):
return rectangle.x * rectangle.y
#==============================================================================
class Triangle(Shape):
def __init__(self, base, height):
self.calculator = triangular_area_calculator
self.base = base
self.height = height
#==============================================================================
def triangular_area_calculator(triangle):
return 0.5 * triangle.base * triangle.height
#==============================================================================
if (__name__ == "__main__"):
x = 4
y = 5
rect = Rectangle(x, y)
print(str(x) + " x " + str(y) + " Rectangle area: " + str(rect.area()))
base = 4
height = 5
tri = Triangle(base, height);
print(
"Base " +
str(base) +
", Height " +
str(height) +
" Triangle area: " +
str(tri.area())
)