-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_super() function.py
More file actions
170 lines (133 loc) · 4.05 KB
/
29_super() function.py
File metadata and controls
170 lines (133 loc) · 4.05 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
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
# super() function :
# a) problem without super function :
class Employee :
def __init__(self, name, age, eid, sal) :
self.name = name
self.age = age
self.eid = eid
self.sal = sal
def printDetails(self) :
print(f'Employee Name : {self.name}')
print(f'Employee Age : {self.age}')
print(f'Employee ID : {self.eid}')
print(f'Employee Salary : {self.sal}')
print('-'*20)
class Student :
def __init__(self, name, age, sid, course, college) :
self.name = name
self.age = age
self.sid = sid
self.course = course
self.college = college
def printDetails(self) :
print(f'Student Name : {self.name}')
print(f'Student age : {self.age}')
print(f'Student ID : {self.sid}')
print(f'Course : {self.course}')
print(f'College : {self.college}')
print('-'*20)
class Faculty :
def __init__(self, name, age, fid, dept, college) :
self.name = name
self.age = age
self.fid = fid
self.dept = dept
self.college = college
def printDetails(self) :
print(f'Faculty Name : {self.name}')
print(f'Faculty Age : {self.age}')
print(f'Faculty ID : {self.fid}')
print(f'Dept : {self.dept}')
print(f'College : {self.college}')
e = Employee('Rahul', 27, 1024, 10000)
e.printDetails()
s = Student('Priyanka', 23, 1011, 'MCA', 'CEB')
s.printDetails()
f = Faculty('Dr. Chand', 28, 1022, 'CSE', 'CEB')
f.printDetails()
# Solution by using super function :
class Human :
def __init__(self, name, age):
self.name = name
self.age = age
def printDetails(self, name, age) :
print(f'Name : {self.name}')
print(f'Age : {self.age}')
class Employee(Human) :
def __init__(self, name, age, eid, sal) :
super().__init__(name, age)
self.eid = eid
self.sal = sal
def printDetails(self) :
super().printDetails()
print(f'Employee ID : {self.eid}')
print(f'Salary : {self.sal}')
print('-'*20)
class Student(Human) :
def __init__(self, name, age, sid, course, college) :
super().__init__(name, age)
self.sid = sid
self.course = course
self.college = college
def printDetails(self):
super().printDetails()
print(f'Student ID : {self.sid}')
print(f'Course : {self.course}')
print(f'College : {self.college}')
print('-'*20)
class Faculty(Human) :
def __init__(self, name, age, fid, dept, college) :
super().__init__(name, age)
self.fid = fid
self.dept = dept
self.college = college
def printDetails(self):
super().printDetails()
print(f'Faculty ID : {self.fid}')
print(f'Dept : {self.dept}')
print(f'College : {self.college}')
e = Employee('Jack', 29, 1024, 15000)
e.printDetails()
s = Student('Archana', 24, 1022, 'MBA', 'DTU')
s.printDetails()
f = Faculty('Dr. Suresh', 31, 11234, 'EEE', 'DCE')
f.printDetails()
# call method of a particular class from child class :
class A :
def method1(self) :
print("A class method1")
class B(A) :
def method1(self) :
print('B class method1')
class C(A) :
def method1(self) :
print('C class method1')
A.method1(self)
class D(B,C) :
def method1(self) :
print('D class method1')
B.method1(self) #calling method1() of B class
#A.method1(self)
d = D()
d.method1()
c = C()
c.method1()
# OR,
class A :
def method1(self) :
print("A class method1")
class B(A) :
def method1(self) :
print('B class method1')
class C(A) :
def method1(self) :
print('C class method1')
super().method1()
class D(B,C) :
def method1(self) :
print('D class method1')
super().method1()
d = D()
d.method1()
c = C()
c.method1()