Exposing your Parent
By default, R6 classes only have access to the functionality of their direct parent. To allow access across multiple generations, the intermediate classes need to define an active binding that exposes their parent. This takes the form
active = list(
super_ = function() super
)
Diese Übung ist Teil des Kurses
Object-Oriented Programming with S3 and R6 in R
Anleitung zur Übung
A microwave oven class has been predefined for you in your workspace.
- Extend the fancy microwave oven class by adding an
active
list element. - Add an active binding to expose its parent's functionality.
- The binding should be called
super_
. - It should be a function that takes no arguments and simply returns
super
.
- The binding should be called
- Instantiate a fancy microwave object and assign it to
a_fancy_microwave
. - Call the fancy microwave's
super_
binding.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Expose the parent functionality
fancy_microwave_oven_factory <- R6Class(
"FancyMicrowaveOven",
inherit = microwave_oven_factory,
public = list(
cook_baked_potato = function() {
self$cook(3)
},
cook = function(time_seconds) {
super$cook(time_seconds)
message("Enjoy your dinner!")
}
),
# Add an active element with a super_ binding
___ = ___(
___ = ___
)
)
# Instantiate a fancy microwave
a_fancy_microwave <- ___
# Call the super_ binding
___