Skip to content

Commit 3daa21f

Browse files
authored
Merge pull request mouredev#4915 from Jairo-Alejandro/main
#[28] - [Python]
2 parents 2f424b5 + 407c7a7 commit 3daa21f

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)
3+
4+
Si se tiene una clase base y una subclase que hereda la clase base , se debe poder usar la subclase sin afectar el programa
5+
"""
6+
# Ejemplo incorrecto
7+
class Animal():
8+
9+
def fly(self):
10+
pass
11+
12+
class Bird(Animal):
13+
14+
def fly(self):
15+
print("the bird is fly")
16+
17+
class Penguin(Animal):
18+
19+
def fly(self):
20+
raise Exception("the penguin can't fly")
21+
22+
def fly_animal(animal: Animal):
23+
animal.fly()
24+
25+
my_bird = Bird()
26+
my_penguin = Penguin()
27+
28+
#fly_animal(my_bird)
29+
#fly_animal(my_penguin)
30+
31+
# Ejemplo correcto
32+
33+
class Animal():
34+
def move(self):
35+
pass
36+
37+
class can_fly():
38+
def fly(self):
39+
pass
40+
41+
class Bird(Animal, can_fly):
42+
def move(self):
43+
print("The Bird is moving")
44+
45+
def fly(self):
46+
print("The Bird is fly")
47+
48+
class Penguin(Animal):
49+
def move(self):
50+
print("The penguin is moving")
51+
52+
def move_animal(animal: Animal):
53+
animal.move()
54+
55+
def fly_animal(animal: can_fly):
56+
animal.fly()
57+
58+
my_bird = Bird()
59+
my_penguin = Penguin()
60+
61+
move_animal(my_bird)
62+
move_animal(my_penguin)
63+
64+
fly_animal(my_bird)
65+
66+
# Dificultad extra
67+
68+
class Vehicle():
69+
70+
def accelerate(self):
71+
pass
72+
73+
def brake(self):
74+
pass
75+
76+
class Car(Vehicle):
77+
78+
def accelerate(self):
79+
print("the car is accelerate")
80+
81+
def brake(self):
82+
print("the car is braking")
83+
84+
class Bicycle(Vehicle):
85+
86+
def accelerate(self):
87+
print("the Bicycle is accelerate")
88+
89+
def brake(self):
90+
print("the Bicycle is braking")
91+
92+
class Motorcycle(Vehicle):
93+
94+
def accelerate(self):
95+
print("the Motorcycle is accelerate")
96+
97+
def brake(self):
98+
print("the Motorcycle is braking")
99+
100+
def test_vehicle(vehicle: Vehicle):
101+
vehicle.accelerate()
102+
vehicle.brake()
103+
104+
my_car = Car()
105+
my_bicycle = Bicycle()
106+
my_motorcycle = Motorcycle()
107+
108+
test_vehicle(my_car)
109+
test_vehicle(my_bicycle)
110+
test_vehicle(my_motorcycle)
111+

0 commit comments

Comments
 (0)