ComenzarEmpieza gratis

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
)

Este ejercicio forma parte del curso

Object-Oriented Programming with S3 and R6 in R

Ver curso

Instrucciones del ejercicio

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.
  • Instantiate a fancy microwave object and assign it to a_fancy_microwave.
  • Call the fancy microwave's super_ binding.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# 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
___
Editar y ejecutar código