Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
confuse of load(MAX_PEZ)
what does this do?
I carried on watching the videos so understand where you're coming from on this one! This is about overriding methods.
You originally have a method that sets the
mPezCount member variable to the value stored in the constant
MAX_PEZ. That makes sense, right?
Then, we're creating a new method that takes a number of items to add to the
mPezCount rather than just adding 12, or whatever. So, you have a new method that takes a parameter and add the value of that parameter to the number contained in
mPezCount. Make sense still? It just lets us add 1, or 2, or whatever rather than just
Having two methods called the same thing is fine as they both have different signatures, i.e. their first line is different - one takes a parameter, one does not.
Here, we can build some efficiency, though. You'll have heard of DRY coding (Don't Repeat Yourself) - well, here, we are repeating ourselves a little. Rather than having the first (non-parameter) function change the value of
mPezCount we can use the new function to do that for us. Inside of the old function, we call the new function, passing in
MAX-PEZ as the parameter. The new function then receives this and adds
MAX_PEZ to the value contained in
That's just a little bit slicker and means that only one
load() method changes the value in
OK with that?
P.S. I still have no idea what a Pez Dispenser is or what Pez's are!?