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 12a67a0

Browse files
Add demo of polymorphism.
1 parent 7e1cf9d commit 12a67a0

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

‎SimpleOOP/src/simpleoop.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,34 @@ def buttons(self):
8383
def screen(self):
8484
super().screen() # method to access parent class methods
8585
print('Display is colored as a whole.')
86+
87+
class Rose:
88+
def color(self):
89+
print('Rose is Red.')
90+
def smell(self):
91+
print('Rose has a sweet smell.')
92+
def stem(self):
93+
print('Stem of rose have thorns.')
94+
def location(self):
95+
print('Rose in the garden.')
96+
97+
class Lotus:
98+
def color(self):
99+
print('Lotus is pink.')
100+
def smell(self):
101+
print('Lotus has no smell.')
102+
def stem(self):
103+
print('Stem of lotus has no thorns.')
104+
def location(self):
105+
print('Lotus in the pond.')
86106

107+
def what_location_n_color(lotus):
108+
lotus.location()
109+
lotus.color()
87110

111+
def smell_the_flower(rose):
112+
rose.smell()
113+
88114
def main():
89115
# instantiate: make object of the class
90116
f = Fibonacci(0,1)
@@ -121,5 +147,15 @@ def main():
121147
samsung.buttons()
122148
samsung.screen()
123149

150+
print()
151+
print('Explaining POLYMORPHISM in python:')
152+
print('Python being loosely typed language, polymorphism is inherent in this language.')
153+
redrose = Rose()
154+
redlotus = Lotus()
155+
for o in (redrose, redlotus):
156+
what_location_n_color(o)
157+
smell_the_flower(o)
158+
o.color()
159+
124160

125-
if __name__ == '__main__': main()
161+
if __name__ == '__main__': main()

0 commit comments

Comments
(0)

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