Challenge
Note
Private methods and attributes of the parent class are not available in the child's class.
The Animal
class:
9
1
2
3
4
5
6
7
class Animal:
def __init__(self, name = None, age = None):
self.__name = name
self.__age = age
def move(self):
print('Animal is moving...')
1234567class Animal: def __init__(self, name = None, age = None): self.__name = name self.__age = age def move(self): print('Animal is moving...')
Tarea
Swipe to start coding
- Create a class
Cat
that inherits theAnimal
class. - Create the
eat
method. - Create a
5
kg10
years old'Lola'
cat. - Move the cat (using the
move()
method from theAnimal
class). - 'Feed' the cat (using the
eat()
method from theCat
class).
Solución
99
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
class Animal:
def __init__(self, name = None, age = None):
self.__name = name
self.__age = age
def move(self):
print('Animal is moving...')
# Create a class Cat that inherits the Animal
class Cat(Animal):
def __init__(self, name = None, age = None, weight = 0):
self.__name = name
self.__age = age
# Create the private variable weight
self.__weight = weight
# Create the eat method
def eat(self):
self.__weight += 1
print(f'{self.__name} is {self.__weight} kg now.')
# Create a 10 years old 'Lola' cat
cat = Cat('Lola', 10, 5)
# Use the move() method
cat.move()
# Use the eat() method
cat.eat()
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 2
99
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
class Animal:
def __init__(self, name = None, age = None):
self.__name = name
self.__age = age
def move(self):
print('Animal is moving...')
# Create a class Cat that inherits the Animal
___:
def __init__(self, name = None, age = None, weight = 0):
self.__name = name
self.__age = age
# Create the private variable weight
___ = ___
# Create the eat method
___(self):
self.__weight += 1
print(f'{self.__name} is {self.__weight} kg now.')
# Create a 5 kg 10 years old 'Lola' cat
cat = Cat(___)
# Use the move() method
cat.___()
# Use the eat() method
___
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla