CS61A - Fall 2013 - Object Oriented Programming
A lot of you ask “What is the purpose of @property?” and so I decided to write up a short summary of why we have @property, what it’s used for, and how to use it (correctly). When we define objects, we have several attributes that we can access by using the dot methods associated with our objects. We can access instance variables simply by doing
my_object.my_instance_variable and we can call a method by doing
my_object.call_my_method(...). We usually define our instance variables in our constructors and then modifying them in subsequent methods. However, what happens if I have an instance variable that depends on other instance variables? Should I have to modify them both each time? Based on everything else we’ve learned in this class, your intuition should tell you that if you have to do something twice, you’re probably doing something wrong. Let’s explore that idea with @property.
Below are a list of practice problems, test what you've learned!