Overriding the Cooking Capabilities
Child classes can also extend functionality by overriding methods. They do this by defining methods with the same name as that of the parent.
Child classes can access public methods from their parent class by prefixing the name with super$
.
Este exercício faz parte do curso
Object-Oriented Programming with S3 and R6 in R
Instruções do exercício
A microwave oven has been predefined in your workspace.
- Update the definition of the fancy microwave oven class to include a public element.
- Override the
cook()
method.- The cook method should still take a
time_seconds
argument. - It should pass the
time_seconds
argument to the parent class'cook()
method, … - then display an additional
message()
(docs) stating"Enjoy your dinner!"
.
- The cook method should still take a
- Instantiate a fancy microwave oven object and assign it to
a_fancy_microwave
. - Call the
cook()
method for one second.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Explore microwave oven class
microwave_oven_factory
# Update the class definition
fancy_microwave_oven_factory <- R6Class(
"FancyMicrowaveOven",
inherit = microwave_oven_factory,
# Add a public list with a cook method
___ = ___(
___ = ___
)
)
# Instantiate a fancy microwave
a_fancy_microwave <- ___
# Call the cook() method
___