-
Notifications
You must be signed in to change notification settings - Fork 0
/
Abstraction.py
124 lines (81 loc) · 2.27 KB
/
Abstraction.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from abc import ABC, abstractmethod
# 1. EXAMPLES
print('\n1. Examples')
class AbsClass(ABC):
def print_val(self, x):
print("Passed value: ", x)
@abstractmethod
def task(self):
print("We are inside Absclass task")
class TestClass(AbsClass):
def task(self):
print("We are inside test_class task")
class ExampleClass(AbsClass):
def task(self):
print("We are inside example_class task")
# object of test_class created
test_obj = TestClass()
test_obj.task()
test_obj.print_val(100)
# object of example_class created
example_obj = ExampleClass()
example_obj.task()
example_obj.print_val(200)
print("test_obj is instance of Absclass? ", isinstance(test_obj, AbsClass))
print("example_obj is instance of Absclass? ", isinstance(example_obj, AbsClass))
# 2. EXAMPLES
print('\n2. Examples')
class Bank(ABC):
@staticmethod
def branch(RD):
print("Fees submitted : ", RD)
@staticmethod
@abstractmethod
def bank(RD):
pass
class Private(Bank):
@staticmethod
def bank(RD):
print("Total RD Value here: ", RD)
class XXX(Bank):
@staticmethod
def bank(RD):
print("Total RD Value here:", RD)
Private.bank(500)
XXX.bank(200)
XXX.branch(200)
# 3. EXAMPLES
print('\n3. Examples')
class Geometric(ABC):
length = 1
width = 1
height = 1
def volume(self): # abstract method
return self.length * self.width * self.height
class Rect(Geometric):
length = 4
width = 6
height = 6
def volume(self):
return self.length * self.width * self.height
class Sphere(Geometric):
radius = 8
def volume(self):
return 1.3 * 3.14 * self.radius * self.radius * self.radius
class Cube(Geometric):
Edge = 5
def volume(self):
return self.Edge * self.Edge * self.Edge
class Triangle3D:
length = 5
width = 4
def volume(self):
return 0.5 * self.length * self.width
rr = Rect()
ss = Sphere()
cc = Cube()
tt = Triangle3D()
print("Volume of a rectangle:", rr.volume())
print("Volume of a circle:", ss.volume())
print("Volume of a square:", cc.volume())
print("Volume of a triangle:", tt.volume())