1. Learn
  2. /
  3. Courses
  4. /
  5. Object-Oriented Programming with S3 and R6 in R

Exercise

Attack of the Clones (1)

R6 objects use the same copy by reference behavior as environments. That is, if you copy an R6 object using <- assignment, then changes in one object will be reflected in the copies as well.

a_reference_copy <- an_r6_object

R6 objects have an automatically generated clone() method that is used to create a copy by value, so that changes to one copy do not affect the others.

a_value <- an_r6_object$clone()

Instructions

100 XP

A microwave oven factory has been predefined in your workspace as microwave_oven_factory.

  • Create a MicrowaveOven object and assign it to a_microwave_oven.
  • Copy the microwave into a variable named assigned_microwave_oven using <- assignment.
  • Copy the microwave into a variable named cloned_microwave_oven using a_microwave_oven's clone() method.
  • Change the power_level_watts field of a_microwave_oven to 400.
  • Verify that the power_level_watts fields of a_microwave_oven and assigned_microwave_oven are identical().
  • Verify that the power_level_watts fields of a_microwave_oven and cloned_microwave_oven are not identical().