1
+ // Ejercicio
2
+
3
+ /* Incorrecto */
4
+
5
+ /* class Ball {
6
+ bounce() {
7
+ console.log(`The ball bounce!!!`);
8
+ }
9
+ }
10
+
11
+ class BowlingBall extends Ball {
12
+ bounce() {
13
+ throw new Error(`Bowling ball can't bounce!!!`)
14
+ }
15
+ } */
16
+
17
+ /* const ball = new Ball()
18
+ const bowlingBall = new BowlingBall()
19
+ ball.bounce()
20
+ bowlingBall.bounce() */
21
+
22
+ /* Correcto */
23
+
24
+ class Ball {
25
+ throw ( ) {
26
+ console . log ( `Throw the ball` ) ;
27
+ }
28
+ }
29
+
30
+ class BowlingBall extends Ball {
31
+ throw ( ) {
32
+ console . log ( `Throw the ball toward pins` ) ;
33
+ }
34
+ }
35
+
36
+ let ball = new Ball ( )
37
+ let bowlingBall = new BowlingBall ( )
38
+ ball . throw ( )
39
+ bowlingBall . throw ( )
40
+
41
+ /* Comprobación del principio de sustitución de Likov */
42
+
43
+ ball = new BowlingBall ( )
44
+ bowlingBall = new Ball ( )
45
+ ball . throw ( )
46
+ bowlingBall . throw ( )
47
+
48
+ // Extra
49
+
50
+ class Vehicle {
51
+ constructor ( speed = 0 ) {
52
+ this . speed = speed
53
+ }
54
+
55
+ accelerate ( increment ) {
56
+ this . speed += increment
57
+ console . log ( this . speed ) ;
58
+
59
+ }
60
+ break ( decrement ) {
61
+ this . speed -= decrement
62
+ if ( this . speed <= 0 ) {
63
+ console . log ( this . speed = 0 )
64
+ }
65
+ console . log ( this . speed )
66
+ }
67
+ }
68
+
69
+ class Car extends Vehicle {
70
+ accelerate ( increment ) {
71
+ console . log ( `El carro está acelerando` )
72
+ super . accelerate ( increment )
73
+ }
74
+ break ( decrement ) {
75
+ console . log ( `El carro está frenando` )
76
+ super . break ( decrement )
77
+ }
78
+ }
79
+ class Bike extends Vehicle {
80
+ accelerate ( increment ) {
81
+ console . log ( `La bicicleta está acelerando` )
82
+ super . accelerate ( increment )
83
+ }
84
+ break ( decrement ) {
85
+ console . log ( `La bicicleta está frenando` )
86
+ super . break ( decrement )
87
+ }
88
+ }
89
+ class Motocycle extends Vehicle {
90
+ accelerate ( increment ) {
91
+ console . log ( `La moto está acelerando` )
92
+ super . accelerate ( increment )
93
+ }
94
+ break ( decrement ) {
95
+ console . log ( `La moto está frenando` )
96
+ super . break ( decrement )
97
+ }
98
+ }
99
+
100
+ function testVehicle ( vehicle ) {
101
+ vehicle . accelerate ( 2 )
102
+ vehicle . break ( 1 )
103
+ }
104
+
105
+ const car = new Car ( )
106
+ const bike = new Bike ( )
107
+ const motocycle = new Motocycle ( )
108
+
109
+ testVehicle ( car )
110
+ testVehicle ( bike )
111
+ testVehicle ( motocycle )
0 commit comments