December 2017
Beginner to intermediate
470 pages
12h 29m
English
Now, we will see how S3 handles mutability and encapsulation. To do so, we will print the a value in the rectangle, modify it, and print it again. As you can see, we are able to modify it, and from that point on we get a different result, and we do so without any method calls. That's a very risky thing to do, and you should definitely wrap this type of behavior in method calls:
print(S3_rectangle$a)#> [1] 2S3_rectangle$a <- 1print(S3_rectangle$a)#> [1] 1
The proper way of modifying an object would be through some type of setter function. The set_color.S3Rectangle() method will be used to modify the rectangle's color, by receiving a S3Rectangle and a ...