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$
.
Diese Übung ist Teil des Kurses
Object-Oriented Programming with S3 and R6 in R
Anleitung zur Übung
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.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# 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
___