-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.oops_pillars_python.py
More file actions
100 lines (64 loc) · 1.5 KB
/
16.oops_pillars_python.py
File metadata and controls
100 lines (64 loc) · 1.5 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
# Example 1: Use of Inheritance in Python
print('>>>> Example - 1 >>>>')
# Parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")
# Child class
class Penguin(Bird):
def __init__(self):
# Call parent class Constructor.
super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
# Example 2: Data Encapsulation in Python
print('\n')
print('>>>> Example - 2 >>>>')
class Computer:
def __init__(self):
self.__max_price = 900
def sell(self):
print("Selling Price: {}".format(self.__max_price))
def setMaxPrice(self, price):
self.__max_price = price
c = Computer()
c.sell()
# change the price
c.__max_price = 1000
c.sell()
# using setter function
c.setMaxPrice(1000)
c.sell()
# Example 3: Using Polymorphism in Python
print('\n')
print('>>>> Example - 3 >>>>')
class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
bird.fly()
# instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(peggy)