-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathinheriting-init-constructor-1.py
More file actions
46 lines (33 loc) · 904 Bytes
/
inheriting-init-constructor-1.py
File metadata and controls
46 lines (33 loc) · 904 Bytes
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
# inheriting-init-constructor-1.py
# This is a normal inheritance example from which we build
# the next example. Make sure to read and understand the
class Animal(object):
def __init__(self, name, thing):
self.name = name
self.thing = thing
class Dog(Animal):
def fetch(self):
print("%s goes after the %s" % (self.name, self.thing))
dog = Dog("Tuffy","Ball")
print("The dog's name is %s and he plays %s" % (dog.name, dog.thing))
dog.fetch()
'''
O/P-
The dog's name is Tuffy and he plays Ball
Tuffy goes after the Ball
'''
#ANOTHER WAY TO ACCESS
class Animal(object):
def __init__(self, name):
self.name = name
class Dog(Animal):
def fetch(self, thing):
print("%s goes after the %s" % (self.name, thing))
d = Dog("TUFFY")
print("The dog's name is", d.name)
d.fetch("Ball")
'''
O/P-
The dog's name is TUFFY
TUFFY goes after the Ball
'''