-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.inheritance.py
99 lines (77 loc) · 2.79 KB
/
14.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
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
# inheritance
'''
fucntion
class: fucntion => mehtod
Inheritance
Parent
child
'''
# parent class
class MondayClass:
def __init__(self, name1, name2):
self.n1 = name1
self.n2 = name2
def mondayClassOutput(self):
print('Hi, ',self.n1,' and ',self.n2)
# class obj
# myobj = MondayClass('Ayman','Megha')
# myobj.mondayClassOutput()
# child class
class TusedayClass(MondayClass):
# override
def __init__(self, name1, name2, message):
# remeber parent class have 2 arguments and in case of child casls it's only recive 2 arguments for parent class.
# thats why these 2 arguemtns must pass to parent class vie child class
# MondayClass.__init__(self, name1, name2)
# short cut or use super() method to pass arguemtns to parent class constructer
super().__init__(name1, name2)
self.m = message
def chilMethodOutput(self):
print('\n',self.m)
print('\nHi i am child class method.')
# create child obj
# on TuesdayClass get 2 args which will pass to prent class
# message = input('Enter your message: ')
# childObj = TusedayClass('Ravi','Raj',message)
# childObj.mondayClassOutput()
# # class child calss method
# childObj.chilMethodOutput()
# anohter class
'''
a = parent class
b(a) = child class
c(b) = child class
d(c) = child class
'''
class SubChildClass(TusedayClass):
pass
# crea obj
subChildObj = SubChildClass('Neha','Suman','Hi class')
subChildObj.chilMethodOutput()
# access mondayclass method
subChildObj.mondayClassOutput()
# multilevel inheritance
class MainClass:
def __init__(self, name):
self.n = name
def name(self):
print('\n\nMy name: ',self.n)
# another super class
class OhterMainClass:
def __init__(self, course):
self.c = course
def course(self):
print('My couser: ',self.c)
# child class which use 2 parent classes
class ChildClass(MainClass, OhterMainClass):
def __init__(self, name, course):
MainClass.__init__(self, name)
OhterMainClass.__init__(self, course)
# obj
mychildOBj = ChildClass('Neha', 'Python')
mychildOBj.name()
mychildOBj.course()
'''
Program:
1. creat class of calculater in which you will define individual methods for sum, subtract, multiplication. div and inherit parent class on child class name: Student, in studetn class you will define method to get studet infromation like name, school, class and show the detils then on other methods which is studetnMarks in which you will get studetn subjects mark like hindi, englis, schi etc and using parent class calulate them and also in parent class have method for find percentage of students mark. then also have other child class named Result in which you will show the studetn resutl by muliline stirng on result format as output.
'''