ComenzarEmpieza gratis

Extending the Cooking Capabilities

The child class can extend the functionality of the parent by adding further public methods with names that are different to those available in the parent class.

Public methods can call other public methods by prefixing their name with self$.

Este ejercicio forma parte del curso

Object-Oriented Programming with S3 and R6 in R

Ver curso

Instrucciones del ejercicio

A microwave oven has been predefined in your workspace.

  • Extend the definition of the fancy microwave oven class to include a public element.
  • Add a public cook_baked_potato() method.
    • This should take no arguments.
    • In the body, it should call its own cook() method for 3 seconds.
  • Create a FancyMicrowaveOven object and assign it to a_fancy_microwave.
  • Call a_fancy_microwave's cook_baked_potato() method.

Warning: Do not try to eat baked potatoes after they have been cooked for 3 seconds as they will taste awful and you may suffer food poisoning.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# Explore microwave oven class
microwave_oven_factory

# Extend the class definition
fancy_microwave_oven_factory <- R6Class(
  "FancyMicrowaveOven",
  inherit = microwave_oven_factory,
  # Add a public list with a cook baked potato method
  ___ = ___(
    ___ = ___
    
    
  )
)

# Instantiate a fancy microwave
a_fancy_microwave <- ___

# Call the cook_baked_potato() method
___
Editar y ejecutar código