Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e1cf9d

Browse files
Add demo for inheritance using class Phone and Mobile_phone
1 parent 8a75a72 commit 7e1cf9d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎SimpleOOP/src/simpleoop.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ def set_variable(self, k, v):
6060
print('Key-value argument makes the class model easily scalable in python.')
6161
self.variables[k] = v
6262

63+
class Phone:
64+
def __init__(self):
65+
print('parent class')
66+
67+
def buttons(self):
68+
print('Has buttons to dial a number.')
69+
70+
def screen(self):
71+
print('Phone has a black and white display!')
72+
73+
class Mobile_phone(Phone):
74+
def __init__(self):
75+
print('Child class of Phone')
76+
77+
def internet_connection(self):
78+
print('Can connect to internet using WiFi!')
79+
80+
def buttons(self):
81+
print('Buttons on touch screen! -> Overriding buttons from Parent class Phones!')
82+
83+
def screen(self):
84+
super().screen() # method to access parent class methods
85+
print('Display is colored as a whole.')
86+
87+
6388
def main():
6489
# instantiate: make object of the class
6590
f = Fibonacci(0,1)
@@ -84,4 +109,17 @@ def main():
84109
marty.set_variable('intelligence', 'VeryHighIntell')
85110
print('Getting new value of \'intelligence\':', marty.get_variable('intelligence'))
86111

112+
print()
113+
print('Explaining Inheritance in Python OOP:')
114+
telepone = Phone()
115+
print('Methods of obj telephone:')
116+
telepone.buttons()
117+
telepone.screen()
118+
samsung = Mobile_phone()
119+
print('Methods of derived class:')
120+
samsung.internet_connection()
121+
samsung.buttons()
122+
samsung.screen()
123+
124+
87125
if __name__ == '__main__': main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /