-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEjercicioOOP2.py
76 lines (57 loc) · 1.5 KB
/
EjercicioOOP2.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
class Pets:
dogs = []
def __init__(self, dogs):
self.dogs = dogs
def walk(self):
for dog in self.dogs:
print(dog.walk())
# Parent class
class Dog:
# Class attribute
species = 'mammal'
is_hungry = True
# Initializer / Instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def description(self):
return "{} is {} years old".format(self.name, self.age)
# instance method
def speak(self, sound):
return "{} says {}".format(self.name, sound)
def eat(self):
self.is_hungry = False
def walk(self):
return "{} está caminando".format(self.name)
# Child class (inherits from Dog class)
class RussellTerrier(Dog):
def run(self, speed):
return "{} runs {}".format(self.name, speed)
# Child class (inherits from Dog class)
class Bulldog(Dog):
def run(self, speed):
return "{} runs {}".format(self.name, speed)
my_dogs = [
Bulldog("Tom", 6),
RussellTerrier("Fletcher", 7),
Dog("Larry", 9)
]
my_pets = Pets(my_dogs)
print("Tengo {} perros".format(
len(my_pets.dogs)
))
for dog in my_pets.dogs:
dog.eat()
print("{} tiene {} años".format(
dog.name, dog.age
))
are_my_dogs_hungry = False
for dog in my_pets.dogs:
if dog.is_hungry:
are_my_dogs_hungry = True
if are_my_dogs_hungry:
print("Los perros tienen hambre")
else:
print("Los perros no tienen hambre")
my_pets.walk()