Aan de slagGa gratis aan de slag

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$.

Deze oefening maakt deel uit van de cursus

Object-Oriented Programming with S3 and R6 in R

Cursus bekijken

Oefeninstructies

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!".
  • Instantiate a fancy microwave oven object and assign it to a_fancy_microwave.
  • Call the cook() method for one second.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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
___
Code bewerken en uitvoeren