File tree Expand file tree Collapse file tree 1 file changed +83
-0
lines changed
Roadmap/28 - SOLID LSP/python Expand file tree Collapse file tree 1 file changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ /*
3+ * EJERCICIO:
4+ * Explora el "Principio SOLID de Sustitución de Liskov (Liskov Substitution Principle, LSP)"
5+ * y crea un ejemplo simple donde se muestre su funcionamiento
6+ * de forma correcta e incorrecta.
7+ *
8+ """
9+
10+ # Forma correcta
11+ class Bird :
12+ def fly (self ):
13+ return "Flying"
14+
15+
16+ class Sparrow (Bird ):
17+ pass
18+
19+ # Forma incorrecta
20+ class Ostrich (Bird ):
21+ def fly (self ):
22+ raise NotImplementedError ("Ostriches can't fly" )
23+
24+
25+ def make_bird_fly (bird ):
26+ try :
27+ return bird .fly ()
28+ except NotImplementedError as e :
29+ return str (e )
30+
31+
32+ sparrow = Sparrow ()
33+ ostrich = Ostrich ()
34+
35+ print (make_bird_fly (sparrow ))
36+ print (make_bird_fly (ostrich ))
37+
38+ """
39+ * DIFICULTAD EXTRA (opcional):
40+ * Crea una jerarquía de vehículos. Todos ellos deben poder acelerar y frenar, así como
41+ * cumplir el LSP.
42+ * Instrucciones:
43+ * 1. Crea la clase Vehículo.
44+ * 2. Añade tres subclases de Vehículo.
45+ * 3. Implementa las operaciones "acelerar" y "frenar" como corresponda.
46+ * 4. Desarrolla un código que compruebe que se cumple el LSP.
47+ */
48+ """
49+
50+ class Vehiculos :
51+ def __init__ (self , name ):
52+ self .name = name
53+
54+ def acelerar (self ):
55+ raise NotImplementedError
56+
57+ def frenar (self ):
58+ raise NotImplementedError
59+
60+
61+ class Auto (Vehiculos ):
62+ def acelerar (self ):
63+ print (f"{ self .name } esta acelerando.." )
64+
65+ def frenar (self ):
66+ print (f"{ self .name } frenando..." )
67+
68+ class Bicicleta (Vehiculos ):
69+ def acelerar (self ):
70+ print (f"{ self .name } esta acelerando" )
71+
72+ def frenar (self ):
73+ print (f"{ self .name } estas frenando" )
74+
75+ def operar_vehiculo (vehiculo ):
76+ vehiculo .acelerar ()
77+ vehiculo .frenar ()
78+
79+ auto1 = Auto ("BMW" )
80+ bici1 = Bicicleta ("BMX" )
81+
82+ operar_vehiculo (auto1 )
83+ operar_vehiculo (bici1 )
You can’t perform that action at this time.
0 commit comments