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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

default values based on their specific data type). The properties defined at the top of the<br />

class have global scope within the class, just like variables declared at the top of a sketch<br />

in <strong>Processing</strong>. Variables declared within the head of a constructor or method (parameters),<br />

or within the constructor/method blocks, are local to that respective structure. This all<br />

works the same way as functions work in <strong>Processing</strong>.<br />

The constructors are simply methods (functions declared within a class) with the same<br />

exact name as the class <strong>and</strong> no specified return type. These special methods are invoked<br />

automatically when an object of the class is created. You can have multiple constructors<br />

(each with the same name as the class) as long as the number <strong>and</strong> type of parameters in<br />

the constructor head (also known as the signature) is different. This also works the same<br />

way as functions work in <strong>Processing</strong>. Here’s an example of multiple constructors for a<br />

Shape class. Notice that each constructor has the same name as the class, but a unique<br />

parameter list. (This code is not intended to be run.)<br />

class Shape{<br />

// 1st constructor<br />

Shape(){<br />

}<br />

// 2nd constructor<br />

Shape(float x, float y){<br />

}<br />

// 3rd constructor<br />

Shape(float x, float y, float w, float h){<br />

}<br />

}<br />

As I mentioned previously, methods are just functions declared within a class. Like functions,<br />

they can return a value or not, <strong>and</strong> they can also accept arguments passed into<br />

them. If a method doesn’t return a value, you declare the method using the void keyword.<br />

If the method returns a value, you need to declare it with the specific data type it returns.<br />

Now you’re ready to look at a complete class. Please note that the class definition is just<br />

one part of the process of using a class. Following my description of the BurritoRecipe<br />

class, I’ll show you how to actually incorporate it into a sketch. The class code is a little<br />

lengthy, but I’ll spend some time going over it.<br />

class BurritoRecipe {<br />

// properties<br />

int size;<br />

String tortillaFlavor;<br />

String meatType;<br />

String beanType;<br />

String[]toppings;<br />

int salsaTemperature;<br />

//3 constructors--default, basic, monster<br />

//default burrito recipe constructor<br />

BurritoRecipe (){<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

305<br />

8

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

Saved successfully!

Ooh no, something went wrong!