-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.py
40 lines (28 loc) · 957 Bytes
/
inheritance.py
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
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length):
super(Square, self).__init__(length, length)
class Triangle:
def __init__(self, base, height):
self.base = base
self.height = height
def area(self):
return 0.5 * self.base * self.height
class RightPyramid(Triangle, Square):
def __init__(self, base, slant_height):
self.base = base
self.slant_height = slant_height
super(Square, self).__init__(base, base)
def area(self):
base_area = super(Square, self).area()
perimeter = super().perimeter()
return 0.5 * perimeter * self.slant_height + base_area
pyramid = RightPyramid(2, 4)
print(pyramid.area())