Over-Overriding the Cooking Capabilities
Once intermediate classes have exposed their parent functionality with super_
active bindings, you can access methods across several generations of R6 class. The syntax is
parent_method <- super$method()
grand_parent_method <- super$super_$method()
great_grand_parent_method <- super$super_$super_$method()
This exercise is part of the course
Object-Oriented Programming with S3 and R6 in R
Exercise instructions
Microwave oven and fancy microwave oven classes have been predefined for you in your workspace. A string ascii_pizza_slice
has also been predefined.
- Explore the other microwave oven factories by printing their names.
- Define a
HighEndMicrowaveOven
class generator usingR6Class()
(docs), and assign it tohigh_end_microwave_oven_factory
.- It should inherit from
FancyMicrowaveOven
. - It should have a
public
list element.
- It should inherit from
- Override the public
cook()
method.- The method should take a single argument named
time_seconds
. - It should call the
MicrowaveOven
'scook()
method, … - then display a pizza slice using
message(ascii_pizza_slice)
(docs).
- The method should take a single argument named
- Instantiate a high-end microwave oven object and assign it to
a_high_end_microwave
. - Call
a_high_end_microwave
'scook()
method to cook for one second.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Explore other microwaves
microwave_oven_factory
fancy_microwave_oven_factory
# Define a high-end microwave oven class
high_end_microwave_oven_factory <- ___(
"___",
___ = ___,
___ = ___(
___ = ___
)
)
# Instantiate a high-end microwave oven
a_high_end_microwave <- ___
# Use it to cook for one second
___