04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

310<br />

In the BurritoRecipe class, I included three constructors, which allows the class to be initialized<br />

three different ways, depending on what arguments are passed to the constructor<br />

when the object is instantiated. Within the constructors’ parentheses are any optional<br />

parameters. When you create a new object, passing arguments to a constructor, there<br />

must be a constructor with the same number <strong>and</strong> type of parameters or the compiler will<br />

spit out an error, which if you remember is similar to how functions work as well. The<br />

following object instantiations are valid, based on the available constructors in the<br />

BurritoRecipe class:<br />

// uses default BurritoRecipe constructor<br />

BurritoRecipe recipe1 = new BurritoRecipe();<br />

//uses regular BurritoRecipe constructor<br />

BurritoRecipe recipe2 = ➥<br />

new BurritoRecipe ("spinach", "pinto", "chicken");<br />

However, the following instantiation will generate a compiler error, as there is no constructor<br />

with an appropriate parameter list:<br />

// generates a compiler error.<br />

BurritoRecipe recipe3 = new BurritoRecipe ("spinach", "chicken");<br />

Within the code blocks (the code between the curly braces) of the bottom two constructors<br />

are a number of assignment lines that initialize the class’s instance properties with the<br />

values passed in via the arguments:<br />

this.tortillaFlavor = tortillaFlavor;<br />

this.beanType = beanType;<br />

this.meatType = meatType;<br />

These lines may look a little odd at first. I remember being pretty confused when I first saw<br />

code like this. In naming the parameters, I used the same names as the properties declared<br />

at the top of the class. This was technically not necessary, <strong>and</strong> I could have made the<br />

names different. However, I actually find it easier to remember what parameter value<br />

initializes what property when they have the same names. Perhaps some of you may be<br />

thinking, “When two different things have the same name, couldn’t that create a problem,<br />

especially if the properties declared up top have global scope <strong>and</strong> can be seen anywhere<br />

within the class?” This is a good question. The answer is yes <strong>and</strong> no. Following, I illustrate<br />

a good way <strong>and</strong> a bad way of performing the constructor initialization when both the<br />

parameters <strong>and</strong> the properties have the same names.<br />

Here’s the good code:<br />

this.tortillaFlavor = tortillaFlavor;<br />

this.beanType = beanType;<br />

this.meatType = meatType;<br />

And here’s the bad code:<br />

tortillaFlavor = tortillaFlavor;<br />

beanType = beanType;<br />

meatType = meatType;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!