-
Notifications
You must be signed in to change notification settings - Fork 0
/
oops.py
208 lines (143 loc) · 4.91 KB
/
oops.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
####Object Oriented Programming####
#defining class animal
class animal:
"Contains animal information of age name and type"
#instance attributes
def __init__(self,name,age,types):
self.name=name
self.age=age
self.types=types
#method in class
def sound(self):
if self.types=="Cat":
print("Meow")
elif self.types=="Dog":
print("bark")
else:
print("No such animal found")
#create objects dog and cat
dog=animal("Draco",10,"Dog")
cat=animal("mcgonnel",15,"Cat")
#accessing Attributes
print("The animal is ",dog.types,"\nHis name is ",dog.name,"\nits age is ",dog.age)
print("The animal is ",cat.types,"\nHer name is ",cat.name,"\nits age is ",cat.age)
#call the method of class animal
cat.sound()
dog.sound()
#Built-In Class Attributes
print("animal.__doc__:", animal.__doc__)
print("animal.__name__:", animal.__name__)
print("animal.__module__:", animal.__module__)
print("animal.__bases__:", animal.__bases__)
print("animal.__dict__:", animal.__dict__)
####Inheritance
class birds(animal):
pass
sparrow=birds("phoenix",20,"bird")
print("The animal is ",sparrow.types,"\nHis name is ",sparrow.name,"\nits age is ",sparrow.age)
#overiding init of parent class
class birds(animal):
def __init__(self,name,age):
self.name=name
self.age=age
sparrow=birds("phoenix",20)
print("The animal is ",sparrow.types,"\nHis name is ",sparrow.name,"\nits age is ",sparrow.age)
#super function
class birds(animal):
def __init__(self,name,age,types):
super().__init__(name,age,types)
sparrow=birds("phoenix",20,"bird")
print("The animal is ",sparrow.types,"\nHis name is ",sparrow.name,"\nits age is ",sparrow.age)
#single level inheritance
class parent:
parent1="anderson"
def __init__(self,name,age,lastname):
self.name=name
self.age=age
self.lastname=lastname
class child(parent):
pass
child1=child("dan",20,child.parent1)
print("Child's Name: ",child1.name,"\nChild's age: ",child1.age,"\nChild's Lastname: ",child1.lastname)
#Mutliple inheritance
class parent1:
parent1="anderson"
def __init__(self,name,age,lastname):
self.name=name
self.age=age
self.lastname=lastname
class parent2:
parent2="Meyers"
def __init__(self,name,age,lastname):
self.name=name
self.age=age
self.lastname=lastname
class child(parent1,parent2):
pass
child1=child("dan",20,child.parent1)
child2=child("Tim",30,child.parent2)
print("1st Child Name: ",child1.name,"\n 1st Child's age: ",child1.age,"\n 1st Child's Lastname: ",child1.lastname)
print("2nd Child Name: ",child2.name,"\n 2nd Child's age: ",child1.age,"\n 2nd Child's Lastname: ",child1.lastname)
#Multilevel inheritance
class grandparent:
grandparent="anderson"
def __init__(self,name,age):
self.name=name
self.age=age
class parent(grandparent):
parent="Dan"
pass
class child(parent):
pass
child=child("Tom",20)
print("Child Name: ",child.name,"\nChild's age: "
,child.age,"\nDad's Name: ",child.parent,"\nGrandDad's Name: ",child.grandparent)
#Hiearchical
class parent:
parent="anderson"
def __init__(self,name,age):
self.name=name
self.age=age
class child1(parent):
pass
class child2(parent):
pass
child1=child1("Tom",20)
child2=child2("Daenarys",20)
print("Son Name: ",child1.name,"\nSon's age: ",child1.age,"\nDaughter's Name: ",child2.name,"\nDaughter's age: ",child2.age,"\nFather's Name: ",child2.parent)
#Hybrid (Multilevel +Multiple)
class grandparent:
grandparent="anderson"
def __init__(self,name,age):
self.name=name
self.age=age
class parent1(grandparent):
parent1="Dan"
pass
class parent2:
parent2="Tina"
pass
class child(parent1,parent2):
pass
child=child("Tom",20)
print("Child Name: ",child.name,"\nChild's age: ",child.age,"\nDad's Name: ",child.parent1,"\nMom's name: ",child.parent2,"\nGrandDad's Name: ",child.grandparent)
#method overriding
class Parent:
def method1(self):
print('Calling parent method')
class Child(Parent):
def method1(self):
print('Calling child method')
c = Child()
c.method1()
####Encapsulation
class parent:
def __init__(self):
self._name="Dan"
class child(parent):
def _init__(self):
parent.__init__(self) #passing parent init method
print(self._name)
object2=child()
object1=child._name #directly accessing the parent attribute through child.
print(object2._name) #accessing parent attribute through parent constructor present in child class