-
Notifications
You must be signed in to change notification settings - Fork 4k
/
mouredev.py
107 lines (73 loc) · 1.83 KB
/
mouredev.py
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
101
102
103
104
105
106
107
"""
Ejercicio
"""
# Incorrecto
class Bird:
def fly(self):
return "Flying"
class Chicken(Bird):
def fly(self):
raise Exception("Los pollos no vuelan")
# bird = Bird()
# bird.fly()
# chicken = Chicken()
# chicken.fly()
# Correcto
class Bird:
def move(self):
return "Moving"
class Chicken(Bird):
def move(self):
return "Walking"
bird = Bird()
print(bird.move())
chicken = Chicken()
print(chicken.move())
bird = Chicken()
print(bird.move())
chicken = Bird()
print(chicken.move())
"""
Extra
"""
class Vehicle:
def __init__(self, speed=0):
self.speed = speed
def accelerate(self, increment):
self.speed += increment
print(f"Velocidad: {self.speed} Km/h")
def brake(self, decrement):
self.speed -= decrement
if self.speed <= 0:
self.speed = 0
print(f"Velocidad: {self.speed} Km/h")
class Car(Vehicle):
def accelerate(self, increment):
print("El coche está acelerando")
super().accelerate(increment)
def brake(self, decrement):
print("El coche está frenando")
super().brake(decrement)
class Bicycle(Vehicle):
def accelerate(self, increment):
print("La bicicleta está acelerando")
super().accelerate(increment)
def brake(self, decrement):
print("La bicicleta está frenando")
super().brake(decrement)
class Motorcycle(Vehicle):
def accelerate(self, increment):
print("La moto está acelerando")
super().accelerate(increment)
def brake(self, decrement):
print("La moto está frenando")
super().brake(decrement)
def test_vehicle(vehicle):
vehicle.accelerate(2)
vehicle.brake(1)
car = Car()
bicycle = Bicycle()
motorcycle = Motorcycle()
test_vehicle(car)
test_vehicle(bicycle)
test_vehicle(motorcycle)